如何在列表中添加列表

时间:2017-04-20 10:07:46

标签: java list

我有这个文件:

MERCEDES 320000 3.5 37000 ABS;GPS;RADIO

我想从它做对象。 我这样做:

private List<Car> cars = new ArrayList<>();
public listOfCars(String fileName){
    try{
        FileReader fr = new FileReader(fileName);
        Scanner sc = new Scanner(fr);

        String[] verse = null;

        int count=0;
        while(sc.hasNextLine()){
            verse = sc.nextLine().split(" ");
            String[] components = verse[4].split(";");
            List<String> eq = new ArrayList<>();
            eq.addAll(Arrays.asList(components));
            cars.add(count, new Car(verse[0], Double.parseDouble(verse[1]), Double.parseDouble(verse[2]), Double.parseDouble(verse[3]), eq));
            count++;
        }

        sc.close();
    }catch(FileNotFoundException e){
        e.printStackTrace();
    }
}

当我制作toString时,我可以看到:

listofCars{cars=[Car{m='MERCEDES', c=320000.0, p=3.5, pp=37000.0}]}

但它应该是:

listofCars{cars=[Car{m='MERCEDES', c=320000.0, p=3.5, pp=37000.0, [ABS, GPS, RADIO}]}

我该怎么做?我在哪里弄错了?

1 个答案:

答案 0 :(得分:0)

class ABS {public ABS() {};};
class GPS {public GPS() {};};
class RADIO {public RADIO() {};};

class Car {
    public String m; //Brand/Marke
    public double c; //Kilometres/Kilometer
    public double p; //Something
    public double pp; //Something
    public List<Object> features; //Extras
    public Car(String m,double c,double p,double pp,List<Object> features) {
        this.m=m;
        this.c=c;
        this.p=p;
        this.pp=pp;
        this.features=features;
    }
    public String toString() {
        String s="Car{m="+m+",c="+Double.toString(c)+",p="+Double.toString(p)+",pp="+Double.toString(pp)+",features=[";
        for (int i=0; i < features.length; i++) {
             Object o=features.get(i);
             if (Object.getClass()==ABS.class) {
                 s+="ABS";
             }
             if (Object.getClass()==GPS.class) {
                 s+="GPS";
             }
             if (Object.getClass()==RADIO.class) {
                 s+="RADIO";
             }
             if (i!=features.length-1) {
                 s+=",";
             }
         }
         return s+"]}"
    }
}
public class App {
private List<Car> cars = new ArrayList<>();
public listOfCars(String fileName){
try{
    FileReader fr = new FileReader(fileName);
    Scanner sc = new Scanner(fr);

    String[] verse = null;

    int count=0;
    while(sc.hasNextLine()){
        verse = sc.nextLine().split(" ");
        String[] components = verse[4].split(";");
        List<Object> eq = new ArrayList<>();
        for (String s:components) {
             if (s.equals("ABS") {
                 eq.add((Object)new ABS());
             }
             if (s.equals("RADIO") {
                 eq.add((Object)new RADIO());
             }
             if (s.equals("GPS") {
                 eq.add((Object)new GPS());
             }
        }
        cars.add(count, new Car(verse[0], Double.parseDouble(verse[1]), Double.parseDouble(verse[2]), Double.parseDouble(verse[3]), eq));
        count++;
    }

    sc.close();
}catch(FileNotFoundException e){
    e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
            listOfCars("list.txt");
        }

    }

使用字符串:

class Car {
    public String m; //Brand/Marke
    public double c; //Kilometres/Kilometer
    public double p; //Something
    public double pp; //Something
    public List<String> features; //Extras
    public Car(String m,double c,double p,double pp,List<String> features) {
        this.m=m;
        this.c=c;
        this.p=p;
        this.pp=pp;
        this.features=features;
    }
    public String toString() {
        String s="Car{m="+m+",c="+Double.toString(c)+",p="+Double.toString(p)+",pp="+Double.toString(pp)+",features=[";
        for (int i=0; i < features.length; i++) {
             s+=features.get(i);
             if (i!=features.length-1) {s+=",";
             }
         }
         return s+"]}"
    }
}
public class App {
private List<Car> cars = new ArrayList<>();
public listOfCars(String fileName){
try{
    FileReader fr = new FileReader(fileName);
    Scanner sc = new Scanner(fr);

    String[] verse = null;

    int count=0;
    while(sc.hasNextLine()){
        verse = sc.nextLine().split(" ");
        String[] components = verse[4].split(";");
        List<Object> eq = new ArrayList<>();
        for (String s:components) {
             eq.add(s);
        }
        cars.add(count, new Car(verse[0], Double.parseDouble(verse[1]), Double.parseDouble(verse[2]), Double.parseDouble(verse[3]), eq));
        count++;
    }

    sc.close();
}catch(FileNotFoundException e){
    e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
        listOfCars("list.txt");
    }

}