简体   繁体   中英

Java ArrayList adding current item to Previous item; remove current item

Purpose of the code is to iterate thru each item in ArrayList> listOfLists and combine previous list to current list, sort the current list and remove the next list (since already combined). This needs to happen until there is only one list left. With that I can spit out contents of ArrayList.get(0) to a file.

listOfLists was defined before code piece. What I am struggling with is: how can alStr1 content be sent back to listOfLists.get(0)?

while ( listOfLists.size() > 1 ) {
    System.out.println(">>>>>>>>>>>>>Iteration"+i);
    Iterator<ArrayList<String>> itr = listOfLists.iterator();
    while(itr.hasNext()) {
        ArrayList<String> alStr1 = itr.next();
        try{
            ArrayList<String> alStr2 = itr.next();
            alStr1.addAll(alStr2);
            Collections.sort(alStr1);
            itr.remove();
        }catch (NoSuchElementException e){
            e.printStackTrace();
            break;
        }
    }
}

Any advise offered is greatly appreciated. Thanks

LOGIC:
------
L1 L2 L3 L4 L5 --> L1+L2 L3+L4 L5 
L1+L2 L3+L4 L5 --> L1+L3 L5 
L1+L3 L5 --> L1+L5 
L1+L5 --> L1

L1 => going to a file.

listOfLists will include these 5 lists:
L1: [100,101,102]
L2: [200,201,202]
L3: [300,301,302]
L4: [400,401,402]
L5: [500,501,502]
Iteration 1:
L1 = L1+L2>> [100,101,102,200,201,202]
L3 = L3+L4>> [300,301,302,400,401,402]
L5 = L5   >> [500,501,502]

Iteration 2:
L1 = L1+L3>> [100,101,102,200,201,202,300,301,302,400,401,402]
L5   >> [500,501,502]

Iteration 3:
L1 = L1+L5>> [100,101,102,200,201,202,300,301,302,400,401,402,500,501,502]

This would explain what I am trying to achieve. Please excuse me for not adding this first.

public static void main(String[] args) {

            List<List<Integer>> listOfList = new ArrayList<List<Integer>>();

            Random rand = new Random(System.currentTimeMillis());

            for (int i = 0; i < 5; i++) {
                List<Integer> list = new ArrayList<Integer>();
                for (int j = 0; j < 5; j++) {
                    list.add(rand.nextInt(1000));
                }
                listOfList.add(list);
            }

            while (listOfList.size() > 1) {

                Iterator<List<Integer>> itr = listOfList.iterator();
                List<Integer> first = itr.next();

                while (itr.hasNext()) {
                    List<Integer> temp = itr.next();
                    first.addAll(temp);
                    itr.remove();
                    Collections.sort(first);
                }
            }

            List<Integer> first = listOfList.get(0);
            for (Integer integer : first) {
                System.out.print(integer + ", ");
            }
        }

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