Java 如何将txt读入arrayList,然后返回arrayList?

Java 如何将txt读入arrayList,然后返回arrayList?,java,Java,我需要将文件读入arrayList并返回arrayList,但我很难将其放入对象。我不断地发现这个错误: 类型不匹配:无法从ArrayList转换为ArrayList public static ArrayList<Names> readEntrees(String fileName) { String filename = "data/names.txt"; ArrayList<String> myNames = new ArrayList<St

我需要将文件读入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里面

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);
}
publicstaticarraylistreadentries(字符串文件名){
字符串filename=“data/names.txt”;
ArrayList myNames=新建ArrayList();
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(“names.txt”);
字符串行=null;
而((line=br.readLine())!=null){
//把每一条线分开@@
String[]elements=line.split(@);
//将字符串[]转换为arraylist
myNames=newarraylist(Arrays.asList(elements));
myNames.add(行);
}
br.close();
报税表(我的姓名);
}
类型不匹配:无法从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);
}

您需要将字符串列表转换为名称列表。因此,让我们使用一个简单的名称类(因为我没有您的类代码,所以我必须弥补这一点):

在这里,我们可以使用
新名称(文本)
构造函数将字符串转换为名称对象。因此,让您的方法返回一个
列表,而不是
数组列表,因为前者将完成后者将完成的所有工作,但具有更大的灵活性(代码到接口,…等等),然后使用流,我们可以将字符串数据转换为
列表

测试程序:

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;
    }
}
import java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.List;
导入java.util.regex.Pattern;
导入java.util.stream.collector;
公共类TestNameList{
公共静态void main(字符串[]args){
//TODO:使用文件的实际路径字符串
字符串fileName=“src/pkg3/Names.txt”;
试一试{
列表名称=读取条目(文件名);
name.stream().forEach(System.out::println);
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共静态列表读取条目(字符串文件名)引发IOException{
列表结果=空;
//要在文本中读取的代码
result=Files.lines(path.get(filename))//从文件中获取每一行
.flatMap(Pattern.compile(“@”)::splitAsStream)//将每行拆分为流
.map(Name::new)//将每个字符串映射到名称中
.collect(Collectors.toList());//收集到列表中
返回结果;
}
}

您需要从读取的每个字符串创建一个名称对象,将其放入
数组列表中并返回它。您有一个
数组列表
,并且您说过将返回
数组列表
名称
类型是什么?要返回
字符串
列表还是
列表
名称中
?此
公共静态ArrayList readentres(字符串文件名){String fileName=“data/Names.txt”
也没有意义。为什么要在执行此操作时抛出此参数?在txt文件中,它显示“john@@paul@@rick”。我需要从txt中读取名称,并将每一行分隔成一个数组。然后返回arrraylistYeah,这应该是BufferedReader br=new BufferedReader(new FileReader(filename));很抱歉,您可以使用
.flatMap(Pattern.compile(@”)::splitAsStream)
而不是lambda(只编译一次模式)和
path.get(filename)
而不是新文件(更短,不确定是否更好)。
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;
    }
}