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

时间:2015-10-12 19:34:29

标签: java arrays arraylist

所以我给了一个数组列表的赋值,我似乎无法弄清楚如何将文本文件中的每个元素添加到数组列表中的单独索引(而索引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)将返回circle,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 ]

所以我相信我将不得不使用个别元素方法?在我将创建的矩形和圆形类中,但我无法弄清楚如何使用单个元素而不是整行来创建arraylist。

1 个答案:

答案 0 :(得分:2)

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

class MyAppl:
    def __init__(self):
            # hastable, lists, tablesize defined 
            self.dir_list = []
            self.dir_list = os.getpids()
            for number in self.dir_list:
                    self.proc_path = /proc/somepath;
                    self.f = open(self.proc_path,'r').read()
                    self.ut = self.f.split()[13]
                    self.st = self.f.split()[12]                 
                    item = Proc_struct.Proc_s(ut,st)
            self.insert(item)

#Hashing here 

def heap(self):
            heap = []
            for i in range(self.tableSize):
                    for j in range(len(self.hashTable[i])):
            heapq.heappush(heap,(self.hashTable[i][j].ut,self.hashTable[i][j].st))
            self.top10ele = (heapq.nlargest(15,heap))

            #for i in range(self.tableSize):
                for k in self.top10ele:
                      self.text.insert(INSERT,k[0],'val')
                      self.text.insert(END, "\t\t")
                      self.text.insert(INSERT,k[1],'val')
                      self.text.insert(END,"\t\t\t")
                      self.text.insert(INSERT,k[2],'val')
                      self.text.insert(END,"\t")
                      self.text.insert(END,"\n")

对于String

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

结果数组为:

Circle,green,false,4.0

这使得分别处理每个元素变得更加容易。