简体   繁体   中英

java - Convert String ArrayList to String Array with bigger size

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.

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.

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

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.

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

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