2

I have an Array and an ArrayList like below

String[] stringArray = new String[] {"a","b","c","d","e"};

List<String> stringList = new ArrayList<>();
stringList.add("p");
stringList.add("q");
stringList.add("r");

I want to copy stringList to stringArray. I tried with ArrayList.toArray() method but it set null to fourth element of Array. Is there any way I can do this by without using a for loop

Expected O/P

{"p","q","r","d","e"}
4
  • What would you like to have as a result? Commented Apr 30, 2015 at 6:30
  • what should happen if stringList contains more values than stringArray? Commented Apr 30, 2015 at 6:44
  • @Original Then complete array should be created by the list Commented Apr 30, 2015 at 6:51
  • ok checkout my updated answer Commented Apr 30, 2015 at 7:24

4 Answers 4

3

If you want to just overwrite existing elements in the stringArray, without touching those at farther indexes, use System#arraycopy(Object, int, Object, int, int):

// Convert list to array
String[] stringListAsArray = stringList.toArray(new String[] {});

// Copy
System.arraycopy(stringListAsArray, 0, 
                 stringArray, 0, stringListAsArray.length);

Also, make sure the stringList is not longer than stringArray or you'll get IndexOutOfBoundsExceptions.

Sign up to request clarification or add additional context in comments.

Comments

2

Array has static memory declaration thus you cannot increase the number cells.

to do this, you can convert the stringArray to list using Arrays.asList(stringArray); then add the two lists and finally convert it back to an array.

Comments

1

ArrayList.toArray behaves as you saw (setting the element following the end of the List to null) :

 * <p>If the list fits in the specified array with room to spare
 * (i.e., the array has more elements than the list), the element in
 * the array immediately following the end of the collection is set to
 * <tt>null</tt>.  (This is useful in determining the length of the
 * list <i>only</i> if the caller knows that the list does not contain
 * any null elements.)

You could convert the List to a new array and then copy that array to the start of the original array.

String arr2 = stringList.toArray(new String[stringList.size()]);
System.arraycopy (arr2, 0, stringArray, 0, arr2.length);

Comments

1

If your stringList contains less or equal number of Strings like your stringArray you can do it like this:

    for (int i = 0; i < stringList.size(); i++) {
        stringArray[i] = stringList.get(i);
    }

Update This way will either update the existing array or use the generated array from the list if it contains more values

    String[] listAsArray = stringList.toArray(new String[stringList.size()]);
    if (listAsArray.length <= stringArray.length) {
        for (int i = 0; i < listAsArray.length; i++) {
            stringArray[i] = listAsArray[i];
        }
    } else {
        stringArray = listAsArray;
    }

Comments

Your Answer

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