Convert String array to ArrayList
To convert a String
array to an ArrayList in Java, you can use the following steps:
- Import the necessary classes:
import java.util.ArrayList;
import java.util.Arrays;
- Create a
String
array:
String[] stringArray = {"item1", "item2", "item3"};
- Use the
asList()
method of theArrays
class to convert theString
array to aList
:
List<String> list = Arrays.asList(stringArray);
- Create an ArrayList from the
List
:
ArrayList<String> arrayList = new ArrayList<>(list);
Alternatively, you can use the addAll()
method of the ArrayList class to add all the elements of the String
array to an ArrayList:
ArrayList<String> arrayList = new ArrayList<>();
arrayList.addAll(Arrays.asList(stringArray));
I hope this helps. Let me know if you have any questions.