简体   繁体   中英

List size zero after adding ito another list, then calling list.clear()

I have a script that attempts to create list ( List<Integer> ) of size 2000 from an array, then add them to a result ( List<List<Integer>> ). It also adds remaining elements to the result list. The code attempts to reuse the list of size 2000 by calling list.clear

Problem is that the first list of size 2000 seems to become lost after adding it to result list. I expected that at end of first if block, displaying result element 0 size would give me 2000, but it gives 0. Why? The rest of the results are puzzling, such as at end of second if block where result contents are 2 lists of size 51.

Please help.

PS - I get expected results if instead of using list.clear() , I use list = new ArrayList<>();

public class Problem {

    public static void main(String[] args) {
        // Create chunks
        List<List<Integer>> masterList = getChunks(getArray(2051));
        // Display size
        System.out.println("masterList: " + masterList.size());
    }
    
    // Create array
    public static Integer[] getArray(int size) {
        Integer[] arr = new Integer[size];
        for (int i = 0; i < size; i++) {
            arr[i] = i;
        }
        return arr;
    }
    
    // From input array, create lists of size 2000 
    public static List<List<Integer>> getChunks(Integer[] arr) {

        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();

        if (arr.length > 2000) {

            for (int i = 0; i < arr.length; i++) {
                // Add to list
                list.add(i);
                
                // If size == 2000, add list to result
                if (i > 0 && i % 1999 == 0) {
                    System.out.println("list if 1: " + list.size());        // 2000
                    result.add(list);
                    list.clear();
                    System.out.println("empty if 1: " + list.isEmpty());    // true
                    System.out.println("result if 1:");
                    display(result);                                        // size-0: 0 <== why???  I expect size-0: 2000
                }
                
                // Add remaining elements to result
                if (i == arr.length-1 && list.size() > 0) {
                    System.out.println("i if 2: " + i);                     // 2050  <-- last element
                    System.out.println("list if 2: " + list.size());        // 51
                    result.add(list);
                    System.out.println("result if 2:");
                    display(result);                                        // size-0: 51, size-1: 51
                    break;                                                  
                }
            } 
        } else {
            System.out.println("arr < 2000");                               // Not called
        }

        System.out.println("end result: " + result.size());                 // 2
        display(result);                                                    // size-0: 51, size-1: 51
        return result;
    }

    public static void display(List<List<Integer>> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println("size-" + i + ": " + list.get(i).size());
        }
    }
}

从程序中删除“list.clear()”,它将返回您的预期结果“2000”

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