有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ArrayList的java打印索引<String[]>

我有一个字符串数组的数组列表。数组本身应该保存传入的每个文本文件字。然后,数组存储在ArrayList的索引中。出于某种原因,它不起作用。每个数组只存储一个单词。我试图实现的是,当我调用ArrayList的索引时,它应该打印出存储在数组中的文本文件中的所有单词。谁能看看这个,给我指出正确的方向吗?非常感谢您的反馈。提前谢谢你

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException{
        String [] files = {"C:\\Users\\Kelvin\\Documents\\Hello.txt", "C:\\Users\\Kelvin\\Documents\\Mountain.txt", "C:\\Users\\Kelvin\\Documents\\Thanks.txt"};
        ArrayList<String[]> list =  new ArrayList<String[]>();
        BufferedReader reader;

        for(int i = 0; i < files.length; i++){
            reader = new BufferedReader(new FileReader(files[i]));
            Scanner in = new Scanner(reader.readLine());
            String [] words = null;
            while(in.hasNext()){
                String inputText = in.next();
                words = inputText.split("[ \n\t\r,.;:!?*--+(){}}]");
            }
            in.close();
            list.add(words);
        }
        System.out.println(Arrays.deepToString(list.toArray()));
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您缺少这一部分:

    while (in.hasNext()) {
        String inputText = in.nextLine();//<  nextLine instead of next
        words = inputText.split("[ \n\t\r,.;:!?* +(){}}]");
        list.add(words);//<     Add your result to your List
    }