List<String> list = ..;
String[] array = list.toArray(new String[0]);
For example:
List<String> list = new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[0]);
The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, and returned. You can pass an empty array as well, but you can also pass an array with the desired size.
Important update: Originally the code above used new String[list.size()]. However, this blogpost reveals that due to JVM optimizations, using new String[0] is better now.
List<String> list = ..;
String[] array = list.toArray(new String[0]);
For example:
List<String> list = new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[0]);
The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, and returned. You can pass an empty array as well, but you can also pass an array with the desired size.
Important update: Originally the code above used new String[list.size()]. However, this blogpost reveals that due to JVM optimizations, using new String[0] is better now.
An alternative in Java 8:
String[] strings = list.stream().toArray(String[]::new);
Java 11+:
String[] strings = list.toArray(String[]::new);
Videos
Use this code for that,
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class StringArrayTest {
public static void main(String[] args) {
String[] words = {"ace", "boom", "crew", "dog", "eon"};
List<String> wordList = Arrays.asList(words);
for (String e : wordList) {
System.out.println(e);
}
}
}
new ArrayList( Arrays.asList( new String[]{"abc", "def"} ) );
Use like this.
List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");
String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);
for(String s : stockArr)
System.out.println(s);
Try this
String[] arr = list.toArray(new String[list.size()]);
In Java 8 or later:
CopyString listString = String.join(", ", list);
In case the list is not of type String, a joining collector can be used:
CopyString listString = list.stream().map(Object::toString)
.collect(Collectors.joining(", "));
If you happen to be doing this on Android, there is a nice utility for this called TextUtils which has a .join(String delimiter, Iterable) method.
CopyList<String> list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
String joined = TextUtils.join(", ", list);
Obviously not much use outside of Android, but figured I'd add it to this thread...
Given an array of Strings, return an ArrayList containing the same Strings in * the same order array2List( {"Apple", "Orange", "Banana"} ) -> ["Apple", * "Orange", "Banana"] array2List( {"Red", "Orange", "Yellow"} ) -> ["Red", * "Orange", "Yellow"] array2List( {"Left", "Right", "Forward", "Back"} ) -> * ["Left", "Right", "Forward", "Back"] */ public List<String> array2List(String[] stringArray){
return null;
}
I'm not quite understanding the question. Do I have to create a new array with these items and then do an arrayList, or am I just all turned around? This is whats (embarrassingly) throwing me off:
public List<String> array2List(String[] stringArray)
Any help or an explanation would help. I'm just beginning Java and feel a bit overwhelmed with everything. I've read over and over again about arrayLists, and am just stuck. Thank you for your consideration!