元素在ArrayList中读取

时间:2016-11-16 23:41:35

标签: java loops arraylist foreach

我有一个方法,我遍历ArryList并搜索是否找到该元素。问题是它不是ArryList中的每个元素而是输入整个ArrayList。将文件中的文本作为valueOf添加是否有问题?

 public ArrayList<String> findWord(String input) {
    ArrayList<String> search = new ArrayList<>();
    search.add(String.valueOf(workfiles("D:\\Project\\synonyms.txt")));
     String[] inputs = input.split("  ");//Considering delimiter is double space("  ") in user input
     for (String inputEntry : inputs) //Giving each word as input at a time and search for it in word String
    {
        for (String element: search) {  // PROBLEM IS HERE
            System.out.println(element);
                if (element.startsWith(inputEntry) && element.contains("=")) {
                    String[] parts = element.split("=");
                    searchWord.add(parts[1]);
                    System.out.println(searchWord);
                }

        }
           }
        return null;  // RETURNS THE SYNONIM OF THE WORD
    }

Workfiles方法是抽象类

public abstract class WorkWithFiles {
public ArrayList<String> workfiles(String path){
    try {
        FileInputStream fileInputStream = new FileInputStream(path);
        Scanner inputreader = new Scanner(fileInputStream);
        ArrayList<String> sins = new ArrayList();
        while (inputreader.hasNextLine()) {
            String l = inputreader.nextLine();
            sins.add(l);   // REWRITES DATA FROM FILE TO ARRATLIST
        }
        inputreader.close();
        return sins;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("File not found");
        System.exit(0);
    }
    return null;
}

}

UPD 我打印每个元素: enter image description here

0 个答案:

没有答案