Java从ArrayList创建排序的ArrayList数组

时间:2019-04-27 16:27:47

标签: java

我试图从一个现有的ArrayList创建一个ArrayList Array,其中包含ArrayList中的所有项目,但按ArrayList数组中的项目名称排序。 这是我的问题的可视化结果:

ArrayList<Data>:
[String name0, ...]
[String name0, ...]
[String name1, ...]
[String name0, ...]
[String name1, ...]
...

->

ArrayList<ArrayList<Data>>:
0. ArrayList:        1. ArrayList:        ...
[String name0, ...]  [String name1, ...]
[String name0, ...]  [String name1, ...]
[String name0, ...]
...                  ...

这是我已经做的:

private ArrayList<Data> data = new ArrayList<>();
private ArrayList<ArrayList<Data>> separatedData = new ArrayList<>();
private List<String> projectnames = new ArrayList<>();
private DataManager dataManager;

    public void GenerateDataArrays() throws IOException {

        //adds value to allData
        allData = dataManager.ReadData("data.csv");

        //get all project names out of the ArrayList and adds it to the 
        List projectnames
        for (Data data : allData) {
            if (!projectnames.contains(data.getProject())) {
                projectnames.add(data.getProject());
            }
        }

        //init new temporary ArrayList
        ArrayList<Data> pList = new ArrayList<>();

        //clears seperatedData because the method is called everytime a 
        //button is pressed to reload all data
        separatedData.clear();

        //looks for all Arrays in the Arraylist with the same name and 
        //adds each with an equal name to same list
        for (String name : projectnames) {
            pList.clear();
            for(Data data : allData){
                if (data.getProject().equals(name)){
                    pList.add(data);

                }
            }
            seperatedData.add(pList);

        }
    }

dataManager只是在其中读取如下所示的CSV文件:

"name","boolean1","amount","boolean2","date1","date2","date3","shop","reason","person","boolean3"
"SampleName","false","90.0","true","29.04.2019","29.04.2019","29.04.2019","Sampleshop","SampleReason","SamplePerson","true"
"SampleName","false","10.0","false","29.04.2019","29.04.2019","29.04.2019","Sampleshop","SampleReason","SamplePerson","true"
"SampleName2","false","90.0","false","29.04.2019","29.04.2019","29.04.2019","Sampleshop2","SampleReason2","SamplePerson2","true"
"LastProject","false","90.0","false","29.04.2019","29.04.2019","29.04.2019","Sampleshop","SampleReason","SamplePerson","true"
"LastProject","false","60.0","false","29.04.2019","29.04.2019","29.04.2019","Sampleshop","SampleReason","SamplePerson","true"

并将其添加到ReadData()返回的ArrayList中。

但是当我调用ArrayList Array'separatedData'来通过执行以下操作为每个'Data'接收'名称'和'amount'时:

        for (ArrayList<Data> dataAll : separatedData){
            for(Data data : dataAll){
                    System.out.println(data.getProject() + ", " + data.getAmount());
            }
        }

我收到了:

LastProject, 90.0
LastProject, 60.0
LastProject, 90.0
LastProject, 60.0
LastProject, 90.0
LastProject, 60.0

这是1.仅是最新添加的项目,并且是初始金额的三倍(ArrayList allData可以)

1 个答案:

答案 0 :(得分:0)

问题出在这里:

ArrayList<Data> pList = new ArrayList<>();
separatedData.clear();
for (String name : projectnames) {
   pList.clear();
   for(Data data : allData){
        if (data.getProject().equals(name)){
             pList.add(data);

        }
    }
    seperatedData.add(pList);

}

您仅创建一个ArrayList引用的plist,并且在for循环中不断清除相同的ArrayList。此外,您在ArrayList中添加了对separatedData的相同引用。最后,您ArrayList包含对同一ArrayList的多个引用。 正如@BlackPearl所建议的那样,解决方案是每次在for循环中创建一个新列表:

for (String name : projectnames) {
        ArrayList<Data> pList = new ArrayList<>();
        for(Data data : allData){
            if (data.getProject().equals(name)){
                pList.add(data);
            }
        }
        seperatedData.add(pList);

}