简体   繁体   中英

Java create a sorted ArrayList Array from an ArrayList

I am trying to create an ArrayList Array from an existing ArrayList which contains all items from the ArrayList but sorted by the projectname in array of the ArrayList. Here a visualization of my problem:

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, ...]
...                  ...

Here is what I have already done:

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

        }
    }

Where dataManager just reads a CSV file that looks like this:

"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"

and adds it to an ArrayList which is returned by ReadData().

But when I call ArrayList Array 'separatedData' to recieve the 'names' and 'amount' for each 'Data' by doing this:

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

I recieve this:

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

which is 1. only the latest added project and 2. thrice of the initial amount (The ArrayList allData is fine)

The problem lies here :

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

}

You create just one ArrayList referenced by plist and you are constantly clearing the same ArrayList in your for loop. Moreover you add the same reference to ArrayList to your separatedData . In the end you have ArrayList containg multiple references to the same ArrayList . As suggested by @BlackPearl the solution is to create a new list in your for loop every time :

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM