Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8333848

Unexpected output from the List

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Windows 11, Java 17

      A DESCRIPTION OF THE PROBLEM :
      please run this and check the output

      public static List<List<Integer>> printAllCombination2(int[] arr) { //1,2,3,4

      if(arr == null || arr.length<1)
      return new ArrayList<>();
      List<List<Integer>> intlist = new ArrayList<>();

      for(int i=0; i<arr.length; i++) {

      int j=i;

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

      while(j<arr.length) {
      ilist.add(arr[j]);
      intlist.add(ilist);
      j++;
      }

      }

      return intlist;

      }



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the code which has provided in the description, list object is behaving differently

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [2], [2, 3], [2, 3, 4], [3], [3, 4], [4]]
      ACTUAL -
      [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4], [2, 3, 4], [2, 3, 4], [3, 4], [3, 4], [4]]

      ---------- BEGIN SOURCE ----------
      import java.util.ArrayList;
      import java.util.List;

      public class TestProgram {


         
         public static List<List<Integer>> printAllCombination2(int[] arr) { //1,2,3,4

      if(arr == null || arr.length<1)
      return new ArrayList<>();
      List<List<Integer>> intlist = new ArrayList<>();

      for(int i=0; i<arr.length; i++) {

      int j=i;

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

      while(j<arr.length) {
      ilist.add(arr[j]);
      intlist.add(ilist);
      j++;
      }

      }

      return intlist;

      }
         

      public static void main(String[] args) {

      int[] arr = {1,2,3,4};

      System.out.println(" combinations :::: "+printAllCombination2(arr));
      }


      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      import java.util.ArrayList;
      import java.util.List;

      public class TestProgram {


         public static List<List<Integer>> printAllCombination1(int[] arr) {

      if(arr == null || arr.length<1)
      return new ArrayList<>();
      List<List<Integer>> intlist = new ArrayList<>();

      for(int i=0; i<arr.length; i++) { //1,2,3,4

      int j=i;

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

      while(j<arr.length) {
      ilist.add(arr[j]);
      List<Integer> nlist = new ArrayList<>(ilist);
      intlist.add(nlist);
      j++;
      }

      }

      return intlist;

      }
         
         

      public static void main(String[] args) {

      int[] arr = {1,2,3,4};

      System.out.println(" combinations :::: "+printAllCombination1(arr));
      }


      }

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: