[英]Store text file in non-String array
我试图将文本文件中的行存储到一个不是 String 数组的数组中。 返回类型应该是一个 GO 数组(使用构造函数和 get-/set-methods 创建了另一个名为“GO”的类,我将在代码的其他部分使用它们。)
int rad = 0;
GO [] list = new CD [rad];
public GO[] readFile(String filename) throws FileNotFoundException {
Scanner read = new Scanner (new File(filename));
while (read.hasNextLine()==true){
rad++;
read.nextLine();
} read.close();
read = new Scanner(new File(filename));
for (int i=0;i<list.length;i++){
//here the lines of the file should be stored in the "GO"array
// but the declaration cannot convert string to GO[]
list[i]=read.nextLine()
class GO<T>{
private T ref;
public GO(T ref) {
this.ref = ref;
}
}
您应该更改您的 GO 类以使用泛型,并且您可以使用这种方法为 Go Array 添加值。
list[i] = new GO(read.nextLine());
通过将类更改为泛型。 您可以通过将任何对象传递给构造函数来创建 Go 类的实例。
您需要自己解析生成的String
并从中构建一个GO
对象,这可以是一个单独的方法来保持干净。 获取对象变量的值并适当地使用设置器。
如果您的程序正在写入文件的内容,那么最好让GO
实现Serializeable
并保存/加载对象本身的状态,而不是制作文本。 如果没有,以上是您唯一的选择,真的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.