如何在向另一个Class的构造函数添加内容时使用ArrayList?

时间:2014-04-22 09:44:58

标签: java arraylist constructor io

我尝试创建一个简单的预订系统,我们将读取一个文件,然后我们将添加火车,公共汽车等,然后我们将编写所有内容进行输出。

import java.io.*;
import java.util.*;
public class Company
{

private static ArrayList<Bus> bus = new ArrayList<Bus>();
static int buscount = 0, traincount = 0;
public static void main (String[] args) throws IOException
{

    FileParser();
}
public Company()
{

}

public static void FileParser()
{

     try { 
          File file = new File(); //i fill this later
          File file2 = new File(); // i fill this later
          FileInputStream fis = new FileInputStream(file); 
          FileOutputStream fos = new FileOutputStream(file2); 
          BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 
          BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); 
          String line; 
          while ((line = br.readLine()) != null) 
        { 
            String[] splitted = line.split(",");
            if(splitted[0].equals("ADDBUS"))
            {
                bus.add(buscount) = Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]);
            }

        }
     }
        catch (FileNotFoundException fnfe) { 

        } 
        catch (IOException ioe) { 

        } 
     }
}

我尝试逐行读取文件。例如,其中一行是“ADDBUS,78KL311,10,140,​​54”我将行拆分为“,”然后我尝试将每个数组添加到总线'类'构造函数中,但我无法弄明白。

我的公共汽车类就像` 公共等级公共汽车延伸车辆{

private String command;
private String busName;
private String busPlate;
private String busAge;
private String busSpeed;
private String busSeat;

public Bus(String command, String busname, String busplate, String busage, String busspeed, String busseat)
{
    this.command = command;
    this.busName = busname;
    this.busPlate = busplate;
    this.busAge = busage;
    this.busSpeed = busspeed;
    this.busSeat = busseat;
}

public String getBusName() {
    return busName;
}

public void setBusName(String busName) {
    this.busName = busName;
}

public String getBusPlate() {
    return busPlate;
}

public void setBusPlate(String busPlate) {
    this.busPlate = busPlate;
}

public String getBusAge() {
    return busAge;
}

public void setBusAge(String busAge) {
    this.busAge = busAge;
}

public String getBusSpeed() {
    return busSpeed;
}

public void setBusSpeed(String busSpeed) {
    this.busSpeed = busSpeed;
}

public String getBusSeat() {
    return busSeat;
}

public void setBusSeat(String busSeat) {
    this.busSeat = busSeat;
}

public String getCommand() {
    return command;
}

public void setCommand(String command) {
    this.command = command;
}
   } 

有人能给我一个解决这个问题的方法吗?

谢谢,

5 个答案:

答案 0 :(得分:0)

您可以像这样添加ArrayList项目

bus.add( new Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));

new构造函数调用之前,您缺少Bus个关键字。然后你可以递增计数器(或做任何事情)

bus.add( new Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));
buscount++;

答案 1 :(得分:0)

尝试添加新的总线(...)

  

bus.add(新的   总线(分裂[0],分裂[1],分裂[2],分裂[3],分裂[4],分裂[5]));

答案 2 :(得分:0)

您缺少关键字new来创建类的新实例:

bus.add(new Bus(...));

答案 3 :(得分:0)

据我了解,如果您想调用构造函数,则需要调用new Bus(parms)。 当你说new时,它会调用你班级的构造函数


当你再次说this()时,它会打电话给封闭的课程&#39;构造函数


如果你说super()它会打电话给超级班级&#39;构造

答案 4 :(得分:0)

如果你想通过计数器将它变成地图顺序,你可以使用它:

  

Map(整数,总线)busPosition = new HashMap&lt;&gt;();

     

busPosition.put(buscount,new   总线(分裂[0],分裂[1],分裂[2],分裂[3],分裂[4],分裂[5]));