toArray with parameter is not working as expected

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P4
    • None
    • Affects Version/s: 8u212
    • Component/s: core-libs

      A DESCRIPTION OF THE PROBLEM :
      I'm using the ArrayList toArray method with parameter. The array passed to method toArray is big enough to "force" the toArray method to use the parameters array for the return.

      The problem is that the value at the index = list.size overiting the arrays value with null (see code bellow value "magic").

      import java.util.*;


      public class ArrayesTest {

      public static void main(String[] args) {

      List<String> list = new ArrayList<>();
      list.add("l1");
      list.add("l2");


       
      String [] a = new String[5];


      a[2] = "magic";
      a[3] = "a1";
      a[4] = "a2";

      String[] stringArray = list.toArray(a);
      System.out.println(stringArray.length);

      for(String s : a)
      System.out.println(s);

      }

      }



      FREQUENCY : always


            Assignee:
            Pallavi Sonal (Inactive)
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: