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

toArray with parameter is not working as expected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u212
    • 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


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

              Created:
              Updated:
              Resolved: