gpt4 book ai didi

java - Arraylist求助,如何将单个对象添加到列表中?

转载 作者:行者123 更新时间:2023-12-02 04:18:04 25 4
gpt4 key购买 nike

所以我被分配了数组列表的作业,但我似乎不太清楚如何将文本文件中的每个元素添加到数组列表中的单独索引(而不是索引 0 是整个第一行) ,索引 1 是第二整行等)。

所以我目前拥有的代码是

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;


public class hw2redo
{
public static void main(String args[]) throws FileNotFoundException
{
//Scan file for data
GeometricObject g = null;
BufferedReader file = new BufferedReader(new FileReader("file.txt"));
Scanner diskScanner = new Scanner(file);
//Create dynamic array list
ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
//Scan data and add data to list
while(diskScanner.hasNext())
{
String geolist = diskScanner.next();
g = recreateObject(geolist);

list.add(g);

}

showObjects(list);
System.out.println("");


}
private static GeometricObject recreateObject(String data)
{
GeometricObject object = new GeometricObject(data);
return object;
}
private static void showObjects(ArrayList<GeometricObject> list)
{
for(GeometricObject o : list)
{

System.out.println(o);
System.out.println(list.get(0));

}

}


}
class GeometricObject
{

private String data;
public GeometricObject()
{

}

public GeometricObject(String data)
{
this.data = data;
}

@Override
public String toString()
{
return data;
}


}
class SimpleCircle extends GeometricObject
{
double radius;

/** Construct a circle with radius 1 */
SimpleCircle()
{
radius = 1;
}

/** Construct a circle with a specified radius */
SimpleCircle(double newRadius)
{
radius = newRadius;
}

/** Return the area of this circle */
double getArea()
{
return radius * radius * Math.PI;
}


/** Return the perimeter of this circle */
double getPerimeter()
{
return 2 * radius * Math.PI;
}

/** Set a new radius for this circle */
void setRadius(double newRadius)
{
radius = newRadius;
}
}

我的 file.txt 包含

Circle,green,false,4.0
Circle,blue,false,2.0
Circle,blue,true,7.0
Rectangle,orange,true,10.0,6.0
Rectangle,green,false,5.0,11.0
Rectangle,red,true,14.0,12.0

所以,当我运行我的代码时,我的输出是

Circle,green,false,4.0
Circle,green,false,4.0
Circle,blue,false,2.0
Circle,green,false,4.0
Circle,blue,true,7.0
Circle,green,false,4.0
Rectangle,orange,true,10.0,6.0
Circle,green,false,4.0
Rectangle,green,false,5.0,11.0
Circle,green,false,4.0
Rectangle,red,true,14.0,12.0
Circle,green,false,4.0

这正如我所期望的,因为它将索引 0 作为整个第一行。我的问题是是否有任何方法可以将每个单独的元素添加到整个新数组中。例如 get(0) 将返回圆形,get(1) 将返回绿色等。

作业要求我的输出看起来像

csu.Lin.Circle@55f96302
GeometricObject [color=red, filled=false, dateOfCreation=Wed Feb 11 12:21:51 EST
2015]
Circle [ radius= 4.0 Area=50.27 Perimeter=25.13 ]

所以我相信我必须使用单独的元素方法?在我将创建的矩形和圆形类中,但我无法弄清楚如何使用单个元素而不是整行创建数组列表。

最佳答案

在Java中,string类中有一个方法叫做split 。您可以使用它将输入字符串拆分为字符串元素数组,如下所示:

String[] elements = geolist.split(",");

对于字符串

Circle,green,false,4.0

生成的数组将是:

{"Circle","green",false","4.0"}

这使得单独处理每个元素变得更加容易。

关于java - Arraylist求助,如何将单个对象添加到列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33088864/

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