簡體   English   中英

如何將 txt 讀取到 arrayList 然后返回 arrayList?

[英]How to read txt to arrayList then return arrayList?

我需要將文件讀入 arrayList 並返回 arrayList 但我很難放入 object。 我不斷收到此錯誤:

類型不匹配:無法從 ArrayList 轉換為 ArrayList

*不允許更改方法

我試過把 ArrayList 放在 ArrayList 里面

public static ArrayList<Names> readEntrees(String fileName) {
    String filename = "data/names.txt";

    ArrayList<String> myNames = new ArrayList<String>();

    try {
    BufferedReader br = new BufferedReader(new FileReader("names.txt"));

    String line = null;

    while ((line = br.readLine()) != null) {
        //take each line and split them around @@
        String[] elements = line.split("@@");
        //convert the string[] to an arraylist
        myNames = new ArrayList< String>(Arrays.asList(elements));
        myNames.add(line);
    }

    br.close();
    return (myNames);
}

類型不匹配:無法從 ArrayList 轉換為 ArrayList

您需要將字符串列表轉換為名稱列表。 所以,讓我們使用一個簡單的名稱 class (我必須彌補這個,因為我沒有你的 class 代碼):

public class Name {
    String name;

    public Name(String name) {
        this.name = name;
    } 

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Name [name=" + name + "]";
    }    
}

在這里,我們可以使用new Name(text)構造函數將 String 轉換為 Name object。 因此,讓我們讓您的方法返回List<Name> ,而不是ArrayList<Name> ,因為前者將完成后者將做的所有事情,但具有更大的靈活性(接口代碼等),然后使用 Streams,我們可以將 String 數據轉換為List<Name>

public static List<Name> readEntries(String filename) throws IOException {

    // String filename = "data/names.txt"; // this makes **no sense**. get rid of it

    List<Name> result = null;
    // code to read in text
    File file = new File(filename);
    result = Files.lines(file.toPath()).map(Name::new).collect(Collectors.toList());

    return result;
}

代碼更正為使用"@@"分割每一行,並根據AjahnCharles進行了改進:

public static List<Name> readEntries(String filename) throws IOException {
    List<Name> result = null;
    // code to read in text
    result = Files.lines(Paths.get(filename)) // get each line from file
            .flatMap(Pattern.compile("@@")::splitAsStream) // split each line into stream
            .map(Name::new) // map each String into Name
            .collect(Collectors.toList()); // collect into List

    return result;
}

測試程序:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class TestNameList {
    public static void main(String[] args) {
        // TODO: use actual path String to file
        String fileName = "src/pkg3/Names.txt";
        try {
            List<Name> names = readEntries(fileName);
            names.stream().forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static List<Name> readEntries(String filename) throws IOException {
        List<Name> result = null;
        // code to read in text
        result = Files.lines(Paths.get(filename)) // get each line from file
                .flatMap(Pattern.compile("@@")::splitAsStream) // split each line into stream
                .map(Name::new) // map each String into Name
                .collect(Collectors.toList()); // collect into List

        return result;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM