如何获取 java 中数组列表中每个单词的第一个和最后一个字母

how to get first and last letter of every word in an arraylist in java

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();
        }
    }

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

您可以在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

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, and should be avoided. The char type cannot represent even half of the characters defined in Unicode.

代码点

Unicode Consortium has assigned an integer number to each of the 143,859 characters recognized so far (Unicode 13.0, 2020-03). These numbers range from zero to just over a million. These numbers are known as code points.

您可以获得分配给字符串中每个字符的代码点编号流。调用 String#codePoints returns an IntStream. We can 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 ] ) );
    }
}