Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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无法在构造函数中初始化ArrayList变量_Java_Oop_Variables_Arraylist_Constructor - Fatal编程技术网

Java无法在构造函数中初始化ArrayList变量

Java无法在构造函数中初始化ArrayList变量,java,oop,variables,arraylist,constructor,Java,Oop,Variables,Arraylist,Constructor,我有几种不同类型的数组列表 每个存储从.csv文件分割的数据 //NOTE: this is for understanding, syntax may not be correct. ArrayList<String> name = {item1, item2, item3, item4}; ArrayList<String> type = {type1, type2, type3, type4}; ArrayList<Double> price = {pr

我有几种不同类型的数组列表

每个存储从.csv文件分割的数据

//NOTE: this is for understanding, syntax may not be correct.
ArrayList<String> name = {item1, item2, item3, item4};
ArrayList<String> type = {type1, type2, type3, type4};
ArrayList<Double> price = {price1, price2, price3, price4};
ArrayList<Integer> qty = {qty1, qty2, qty3, qty4};

一个装鸡蛋的盒子。。。这不是一个鸡蛋

字符串列表。。。不是字符串

意思是:不能直接从单个字符串创建字符串列表。只能将字符串添加到现有列表中。就像在你的蛋盒里放一个鸡蛋

你需要:

type = new ArrayList<>();
type.add(t);

您的其他带有项的代码正在工作,因为
general.type.get(i)
返回一个字符串对象;这正是项构造函数所期望的—一个字符串对象。

您正试图将ArrayList初始化为字符串。如果你告诉我你真正想要实现什么,我可以编辑代码来实现

项目类别

public class Item {
    public static ArrayList<String> type_list = new ArrayList<>();
    public static ArrayList<String> name_list = new ArrayList<>();
    public static ArrayList<Double> price_list = new ArrayList<>();
    public static ArrayList<Integer> qty_list = new ArrayList<>();

    public String type;
    public String name;
    public Double price;
    public Integer qty;

    public Item (String type, String name, Double price, Integer qty){
        this.type = type;
        this.name = name;
        this.price = price;
        this.qty = qty;
    }
}
公共类项目{
public static ArrayList type_list=new ArrayList();
public static ArrayList name_list=new ArrayList();
public static ArrayList price_list=new ArrayList();
公共静态ArrayList数量\ u列表=新建ArrayList();
公共字符串类型;
公共字符串名称;
公开双价;
公共整数数量;
公共项目(字符串类型、字符串名称、双倍价格、整数数量){
this.type=type;
this.name=名称;
这个价格=价格;
该数量=数量;
}
}
主类

public class Item {
    public static ArrayList<String> type_list = new ArrayList<>();
    public static ArrayList<String> name_list = new ArrayList<>();
    public static ArrayList<Double> price_list = new ArrayList<>();
    public static ArrayList<Integer> qty_list = new ArrayList<>();

    public String type;
    public String name;
    public Double price;
    public Integer qty;

    public Item (String type, String name, Double price, Integer qty){
        this.type = type;
        this.name = name;
        this.price = price;
        this.qty = qty;
    }
}
公共静态void main(字符串参数[]){ ArrayList items_list=新建ArrayList()

Item.type_list.add(“type1”);
项目。类型列表。添加(“类型2”);
项目。类型列表。添加(“类型3”);
项目。类型列表。添加(“类型4”);
项目名称。添加(“名称1”);
项目名称。添加(“名称2”);
项目名称。添加(“名称3”);
项目名称。添加(“名称4”);
项目.价格清单.添加(价格1);
项目。价格清单。添加(价格2);
项目.价格清单.添加(价格3);
项目.价格清单.添加(价格4);
物料数量清单新增(数量1);
项目数量列表添加(qty2);
物料数量清单新增(数量3);
物料数量清单新增(qty4);
对于(int i=0;i

}

我们无法回答您的问题,因为我们不知道
项是什么,或者它应该做什么。它的
类型
名称
价格
,和
数量
字段似乎都是值的数组列表,但构造函数的参数只键入了一个值。我看到了混淆,我现在就编辑。你明白我想要实现什么了。我试图从不同的ArrayList中获取每个元素,并将它们调用到构造函数中。我的最终目标是创建4个不同的对象来存储一个项目的(类型、名称、价格、数量),以便我可以按类型对它们进行排序。我知道我有点困惑。
type = new ArrayList<>();
type.add(t);
type = Arrays.asList(t);
public class Item {
    public static ArrayList<String> type_list = new ArrayList<>();
    public static ArrayList<String> name_list = new ArrayList<>();
    public static ArrayList<Double> price_list = new ArrayList<>();
    public static ArrayList<Integer> qty_list = new ArrayList<>();

    public String type;
    public String name;
    public Double price;
    public Integer qty;

    public Item (String type, String name, Double price, Integer qty){
        this.type = type;
        this.name = name;
        this.price = price;
        this.qty = qty;
    }
}
Item.type_list.add("type1");
Item.type_list.add("type2");
Item.type_list.add("type3");
Item.type_list.add("type4");

Item.name_list.add("name1");
Item.name_list.add("name2");
Item.name_list.add("name3");
Item.name_list.add("name4");

Item.price_list.add(price1);
Item.price_list.add(price2);
Item.price_list.add(price3);
Item.price_list.add(price4);

Item.qty_list.add(qty1);
Item.qty_list.add(qty2);
Item.qty_list.add(qty3);
Item.qty_list.add(qty4);

for(int i = 0; i < Item.type_list.size(); i++) {
    System.out.println(.....);
}