Convert an Arraylist<String> into a String[]

Asked

Viewed 613 times

3

I need to pass on the values that are in my ArrayList<String> for a String[] because I need to make a setInputData("INPUT",input).

I mean, I want to convert the next ArrayList<String> with the other values for a String[]:

ArrayList<String> list = new ArrayList<>();
list.add("NOME 1");
list.add("NOME 2");
list.add("NOME 3");

Convert to:

String[] input = new String[0]

I’m trying some things, but I’m afraid I’m doing it wrong.

I tried that:

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

But I don’t know if you’re right because my question is what is the need to put the number 0 between [0] in the new String[0] and if, doing so, it will be that the conversion will stay this way: input = {null,"Nome 1","Nome 2","Nome 3"}?

2 answers

2

Friend, you are not succeeding because you are not passing the size of the List to your Array, you are creating the Array with size 0.
Change your code of:

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

for

String[] input = list.toArray(new String[list.size()]);
  • So in this way, all values that are contained in Arraylist<String> passes to String[] right?

  • 1

    @Even so, all values of your List (Arraylist<String>) go to the array (String[])

  • Thank you very much.... Thanks so much for the help!!

  • 1

    @But if you do list.toArray(new String[0]) also works, see. In this case, if you pass an array with zero size, internalize the method toArray allocates another with the required size as described in documentation

  • @Anyway, I put an answer detailing this behavior

2


In fact input = list.toArray(new String[0]) works without problems, only has a detail.

According to the documentation, the method toArray checks if the list fits the specified array, and if it doesn’t fit, it allocates another array with the required size:

If the list Fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the Runtime type of the specified array and the size of this list

In free translation:

If the list fits in the array, it is returned in this. Otherwise, a new array is allocated, with the same type of the specified array, and the size of this list.

That has a slight implication. If the array is the same size as the list, the elements are copied directly into it, so I don’t even need to pick up the return from toArray:

ArrayList<String> list = new ArrayList<>();
list.add("NOME 1");
list.add("NOME 2");
list.add("NOME 3");
String[] input = new String[list.size()];

// array tem o tamanho da lista, ele é modificado dentro do método
list.toArray(input);
for (String s : input) {
    System.out.println(s); // imprime os 3 nomes
}

Notice that I did not assign the return of toArray for no variable. Since the array is large enough to have all the elements in the list, they are copied directly to the array. This code prints out the 3 names:

NOME 1
NOME 2
NOME 3

But if the array is not large enough, a new array is allocated and returned. The array I passed as argument to toArray is not modified:

ArrayList<String> list = new ArrayList<>();
list.add("NOME 1");
list.add("NOME 2");
list.add("NOME 3");
String[] input = new String[0];
list.toArray(input);
for (String s : input) {
    System.out.println(s);
}

In this case, the array input not large enough to contain all the elements on the list, so toArray allocate another array with the proper size and returns it. But I did not assign this return in any variable, and the array input remains empty. That is why this code does not print anything.

In order for the above code to work properly, I would have to do input = list.toArray(input).


Finally, if the array is larger than the list, the excess elements will be null:

ArrayList<String> list = new ArrayList<>();
list.add("NOME 1");
list.add("NOME 2");
list.add("NOME 3");
String[] input = new String[list.size() + 10];
list.toArray(input);
for (String s : input) {
    System.out.println(s);
}

In this case, the array is larger than the list, and the output is:

NOME 1
NOME 2
NOME 3
null
null
null
null
null
null
null
null
null
null

And in this case, even if I do input = list.toArray(input), the output is also the same. After all, this is what is described in the documentation: if the list fits in the array, the elements are copied directly to this one. Then in this case the returned array is the same that was passed to toArray, since it has plenty of space to contain all the elements of the list.


We can also do another test, to confirm whether another array is allocated or not, taking the return of toArray and comparing with the original array, to know if in fact another array is being returned or the same:

ArrayList<String> list = new ArrayList<>();
list.add("NOME 1");
list.add("NOME 2");
list.add("NOME 3");

String[] input = new String[0];
String[] other = list.toArray(input);
System.out.println(other == input);

In this case, the code prints false, as well input not large enough to have all the elements on the list, toArray allocates another array (which is returned and assigned in the variable other).

But if the size of input for list.size() (or any value greater than that), then the code prints true, because now the array has space for all elements of the list, no new array is allocated.


In short, if you make any of the alternatives below:

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

Both work. The difference is that the first will create 2 arrays: one with zero size, and the other that is allocated internally by toArray, since the zero-size array has no space for the list elements (except if the list is empty, because it returns the same array). The second alternative will only allocate an array and return it.

  • 1

    Poxa very well explained..... Thank you very much! This knowledge I will carry with me forever. You kept well the subject and went much further.... I thank you so much for sharing your knowledge with me and the community....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.