【问题标题】:how to get first and last letter of every word in an arraylist in java如何在java中的arraylist中获取每个单词的第一个和最后一个字母
【发布时间】:2021-08-29 13:45:29
【问题描述】:
public static void ReadKeyWordsFile() {

        
        try {

            ArrayList<String> result = new ArrayList<>();
            File file1 = new File("./files/kywrdsOdd.txt");
            if(file1.length() == 0){
                System.out.println("The files is empty");
            }
            else{
            Scanner scan = new Scanner(file1);
                while (scan.hasNextLine()){
                    result.add(scan.nextLine());  
                    
                }
            System.out.println(result);
            
            for (int counter = 0; counter < result.size(); counter++) {               
                int l = result.size() -1; 
                System.out.println(l);
            }
            scan.close();
            }
        } catch (FileNotFoundException e) {
            //TODO: handle exception
            System.out.println("File not found");
            e.printStackTrace();
        }
    }

但这只会返回数组的大小,而不是其中的单词。 我想从数组中的每个单词中获取第一个和最后一个字母的出现

【问题讨论】:

    标签: java arrays arraylist


    【解决方案1】:

    tl;博士

    int[] codePoints = "?Mask".codePoints().toArray();
    if ( codePoints.length > 0 )
    {
        String firstLetter = Character.toString( codePoints[ 0 ] ) ;
        String lastLetter = Character.toString( codePoints[ codePoints.length - 1 ] ) ;
    }
    

    ? k

    避免char

    在处理单个字符时,请学习使用 Unicode 代码点。 Java 中的char 类型是legacy,应该避免使用。 char 类型甚至不能代表Unicode 中定义的一半字符。

    代码点

    Unicode Consortium 已为目前已识别的 143,859 个字符中的每一个分配了一个整数(Unicode 13.0,2020-03)。这些数字的范围从零到刚刚超过一百万。这些号码称为code points。

    您可以获得分配给字符串中每个字符的代码点编号流。调用 String#codePoints 返回一个 IntStream。我们可以convert that to an array of int values。

    从我们的数组中,我们可以提取每个输入字符串的第一个和最后一个字符的代码点整数。我们通过调用 Character.toString 将这些代码点整数转换回文本字符。

    List < String > list = List.of( "?Alice" , "Bob" , "Carol" );
    for ( String s : list )
    {
        IntStream codePointsStream = s.codePoints();
        int[] codePoints = codePointsStream.toArray();
        if ( codePoints.length > 0 )
        {
            int firstCodePoint = codePoints[ 0 ];
            int lastCodePoint = codePoints[ codePoints.length - 1 ];
            System.out.println( Character.toString( firstCodePoint ) + "/" + Character.toString( lastCodePoint ) );
        }
    }
    

    看到这个code run live at IdeOne.com。

    ?/e
    B/b
    C/l
    

    我们可以缩短此代码,但不一定是改进。

    List < String > list = List.of( "?Alice" , "Bob" , "Carol" );
    for ( String s : list )
    {
        int[] codePoints = s.codePoints().toArray();
        if ( codePoints.length > 0 )
        {
            System.out.println( Character.toString( codePoints[ 0 ] ) + "/" + Character.toString( codePoints[ codePoints.length - 1 ] ) );
        }
    }
    

    【讨论】:

    • 我不敢相信我不知道 char 是遗留物,虽然当你想到它时它有点明显,而且它一定已经存在了很长时间 - 从来没有在任何地方明确提到过它,也从来没有发现要考虑它。
    • @daniu 是的,char 基本上已经被破坏了很多年。该类型被定义为 16 位,足以容纳 64K 的字符,最初的 Unicode 团队天真地认为足以处理包括CJK 在内的大多数语言。在 Java 1.0 之后不久,很明显 Unicode 需要更多字符来处理更多晦涩的汉字、各种学术兴趣以及后来的表情符号。 Java 5 带来了对后来所有 Unicode 版本的正式支持。但是char 类型却步履蹒跚。代码点方法是固定的。
    • @daniu 如需更多信息,请参阅:Unicode Versions Supported in Java-History 和 Supplementary Characters in the Java Platform,由 Sun Microsystems, Inc. 的 Norbert Lindenberg 和 Masayoshi Okutsu 撰写,2004 年 5 月。
    【解决方案2】:

    你可以在Java中使用charAt()方法,

    public static void main(String[] args) throws IOException {
        List<String> list = new ArrayList<>();
        list.add("maneesha");
        list.add("cloud");
        list.add("piercer");
    
        for (String s : list) {
            System.out.println((s.charAt(0)) + " " + (s.charAt(s.length() - 1)));
        }
    }
    

    输出将是,

    m a
    c d
    p r
    

    这里m a的第一个字母是m,最后一个字母是a

    【讨论】:

    • 此代码与 Unicode 中定义的大多数字符中断。试试:list.add( "?maneesha" );
    猜你喜欢
    • 2022-01-13
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多