Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

java - Compress rows with same id

Hi i'm trying to loop through this structure:

id string1 string2 different_string
1  test    test      asd
1  test    test      dsa
2  data    data      qwe
3  info    info      ewq
3  info    info      zxc
3  info    info      qaz

I have rows with the exact same value but one of them is different so I'm trying to compress that data into a single row.

This is my code:

            int actual_id = list.get(0).num(); //I pick the first id = 1
            ArrayList<ArrayList<String>> listOLists = new ArrayList<ArrayList<String>>();

            ArrayList<String> items = new ArrayList<String>();
            for(int i = 0; i < list.size(); i++){
                if(list.get(i).id == actual_id){
                    String str = list.get(i).different_string;
                    items.add(str);
                    listOLists.add(items);
                    items.clear();
                }else {
                    actual_id = list.get(i).id;
                    i--;    
                }
            }
            for(int j = 0; j < listOLists.size(); j++) {
                System.out.println(listOLists);
            }

First I check the id of each row and compare it with the actual value, I'm adding the string to an array and then append it to a list so I can store my data then reset the array to append new data to it and repeat the process, the problem is when I reset the array the loop doesn't seem to add more items to the list, what am I doing wrong?

this is I would like to get something like this:

{1, test, test, {asd, dsa}},{2, data, data, {asd}},{3, info, info, {ewq, zxc,qaz}}
question from:https://stackoverflow.com/questions/65891270/compress-rows-with-same-id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code is a little difficult to follow but from what I can tell, your problem lies in this part of the code:

if(list.get(i).id == actual_id){
                String str = list.get(i).different_string;
                items.add(str);
                listOLists.add(items);
                items.clear();
            }else {

You add items to listOList, but in the next line you clear it. items still refers to the List you just added to listOList so when you clear it, it clears the list in listOList as well. You may want to declare items within the if block so a new List will be created each pass through the loop like this:

if(list.get(i).id == actual_id){
            ArrayList<String> items = new ArrayList<String>();
            String str = list.get(i).different_string;
            items.add(str);
            listOLists.add(items);
        }else {

This way, a new List is being created every iteration through the loop avoiding the issue. I can't tell if this will solve all you're problems but I hope this gets you headed in the right direction.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...