1

I am trying to convert Array list to String array but getting an exception. Can somebody please help me.

Size of ArrayList: 1
Size of String Array: 2

I am using the following code:

String[] StringArray ={};
StringArray = ArrayList.toArray(new String[ArrayList.size()]);

So, the length of StringArray now is 1. But it should be 2. My problem is how can i convert arraylist to StringArray if String Array size is more than the ArrayList.

How can i do that? Please guys help me.

10
  • 1
    That would not compile: toArray is not a static method of the ArrayList class. Is that your real code? Commented May 22, 2013 at 12:00
  • 3
    possible duplicate of convert String arraylist to string array in java? Commented May 22, 2013 at 12:01
  • I think, that there is no statis call... he just named variables ArrayList and StringArray Commented May 22, 2013 at 12:02
  • no, its not a duplicate of the question linked. My problem is how can i convert arraylist to StringArray if String Array size is more than the ArrayList. Guys please help me. Commented May 22, 2013 at 12:04
  • @rockskull please see my comment above Commented May 22, 2013 at 12:05

4 Answers 4

4

If you want to convert ArrayList to a bigger String array, use toArray() and pass the array you want to fill as parameter. If the array size is more than needed, the rest of the elements will be null. If the array is smaller - a new array will be returned with size as list.size.

All taken from javadoc

ArrayList<String> list  = new ArrayList<>();
list.add("abc");

String[] StringArray = new String[2];
StringArray = list.toArray(StringArray);

In that case, even though the list size is 1, StringArray is of size 2, adding null values at the end of the array.

0
0
 String[] arr = new String[list.size()];
 arr = list.toArray(arr);
0

Try to do it like in this tutorial: http://viralpatel.net/blogs/convert-arraylist-to-arrays-in-java/

The size of array defined before .toArray method has no effect on result. After calling .toArray, old array is destroyed by JVM.

0

If we pass an array as argument to method toArray then it populates that with the items of list else it returns an array of Object. We can pass an array of size larger than the list as argument to get an array of greater size. The following example explains it:

     public static void main(String[] args) throws FileNotFoundException {
        List<String> list  = new ArrayList<>();
        list.add("One");
        System.out.println("list Size: " + list.size());

        //If we pass an array as argument it will be filled with items from list and then returned. Here array size is (list.size +2 )
        String[] stringArray = list.toArray(new String[2]);
        System.out.println("stringArray Size: " + stringArray.length);

        //If we do not pass an array as argument we get Object[] of same size
        Object[] objects = list.toArray();
        System.out.println("objects Size: " + objects.length);

        // Java 8 has option of streams to get same size array
        String[] stringArrayUsingStream = list.stream().toArray(String[]::new);
        System.out.println("stringArrayUsingStream Size: " + stringArrayUsingStream.length);
    }

As shown above we have an option of using streams also in Java 8. The output is:

list Size: 1 stringArray Size: 2 objects Size: 1 stringArrayUsingStream Size: 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.