gpt4 book ai didi

java - 如何读取txt到arrayList然后返回arrayList?

转载 作者:行者123 更新时间:2023-12-01 21:56:31 27 4
gpt4 key购买 nike

我需要将文件读入 arrayList 并返回 arrayList,但我在放入对象时遇到困难。我不断收到此错误:

Type mismatch: cannot convert from ArrayList to 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);
}

Type mismatch: cannot convert from ArrayList to ArrayList

最佳答案

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

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)将字符串转换为Name对象。构造函数。因此,让我们让您的方法返回 List<Name> ,不是 ArrayList<Name>因为前者会做后者会做的所有事情,但具有更大的灵 active (接口(interface)代码等),然后使用 Streams,我们可以将字符串数据转换为 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;
}
}

关于java - 如何读取txt到arrayList然后返回arrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58740603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com