Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在列表中添加列表_Java_List - Fatal编程技术网

Java 如何在列表中添加列表

Java 如何在列表中添加列表,java,list,Java,List,我有以下文件: MERCEDES 320000 3.5 37000 ABS;GPS;RADIO 我想让它成为一个物体。 我有: 但它应该是: listofCars{cars=[Car{m='MERCEDES', c=320000.0, p=3.5, pp=37000.0, [ABS, GPS, RADIO}]} 我该怎么做?哪里出错?类ABS{public ABS(){};}; class ABS {public ABS() {};}; class GPS {public GPS() {};

我有以下文件:

MERCEDES 320000 3.5 37000 ABS;GPS;RADIO
我想让它成为一个物体。 我有:

但它应该是:

listofCars{cars=[Car{m='MERCEDES', c=320000.0, p=3.5, pp=37000.0, [ABS, GPS, RADIO}]}
我该怎么做?哪里出错?

类ABS{public ABS(){};};
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");
        }

    }
类GPS{public GPS(){};}; 类无线电{public RADIO(){};}; 班车{ 公共字符串m;//品牌/市场 公共双c;//公里/公里 公共双p;//什么 public-double-pp;//某物 公共列表功能;//附加功能 公共汽车(字符串m、双c、双p、双pp、列表特征){ 这个,m=m; 这个.c=c; 这个,p=p; 这个.pp=pp; 这个。特征=特征; } 公共字符串toString(){ 字符串s=“Car{m=“+m+”,c=“+Double.toString(c)+”,p=“+Double.toString(p)+”,pp=“+Double.toString(pp)+”,features=[”; 对于(int i=0;i
使用字符串:

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");
    }

}
等级车{
公共字符串m;//品牌/市场
公共双c;//公里/公里
公共双p;//什么
public-double-pp;//某物
公共列表功能;//附加功能
公共汽车(字符串m、双c、双p、双pp、列表特征){
这个,m=m;
这个.c=c;
这个,p=p;
这个.pp=pp;
这个。特征=特征;
}
公共字符串toString(){
字符串s=“Car{m=“+m+”,c=“+Double.toString(c)+”,p=“+Double.toString(p)+”,pp=“+Double.toString(pp)+”,features=[”;
对于(int i=0;i
Show your class
car
你很可能在
car.toString()中有一个bug
你还没有显示。我会在你的调试器中逐步检查代码以确认这一点。这是如何回答这个问题的?这里添加了很多行。@AxelH:他没有提供他的汽车类。所以你为他提供了它?除非你认识他或实习医生,否则我不明白你怎么能确定这是他需要的。@AxelH:是的,我知道,可能是y他希望将特征作为字符串进行编辑。
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");
    }

}