This question already has answers here:
Create ArrayList from array
(42 answers)
Closed 8 years ago.
How do I replace the following array with an ArrayList.
Employee[] companyTeam = {manager, engineer1, superviso1, accountant, intern };
You can do something like
List<Employee> companyTeam = Arrays.asList(manager, engineer1, superviso1, accountant, intern);
Employee[] companyTeam = {manager, engineer1, superviso1, accountant, intern };
List<Employee> list=Arrays.asList(companyTeam);// array to List
You already have an array, So you can use Arrays.asList(companyTeam) to convert array to List
Related
This question already has answers here:
Converting array to list in Java
(24 answers)
Java: is there a map function?
(6 answers)
Closed last month.
How do I convert List<String[]> to List<List<String>>?
List<String[]> allData = csv.readAll();
allData needs to be coverted in List<List<String>>.
You can use stream and Arrays::asList which will convert String[] to a List<String>, like this:
List<List<String>> response = allData.stream()
.map(Arrays::asList)
.collect(Collectors.toList()); // or just .toList();
This question already has answers here:
Getting all names in an enum as a String[]
(26 answers)
Closed 3 years ago.
I'm having an Enum array. Now I want to convert it to a String array which contains the names of the enums returned by the method Enum#name(). Here's what I tried so far (The enum is called "Column".):
String[] stringArray = Arrays.asList(Column.values()).toArray(String[]::new);
I'm alway getting an ArrayStoreException. What can I do?
You need to stream the enum in order to first map the enum to String before creating the array:
String[] arrStr = Arrays.stream(FooEnum.values()) // create stream of enum values
.map(e -> e.toString()) // convert enum stream to String stream
.toArray(String[]::new); // convert stream to an array
This question already has answers here:
How to store all ArrayList<ArrayList<String>> values into ArrayList<String>?
(2 answers)
Closed 4 years ago.
I have a source list of type ArrayList<ArrayList<String>>. I want to put all the strings from the source list into a destination list of type ArrayList<String>. How do I do this?
Example:
sourceList = { {"a","b"}, {"c","b"} }
destinationList = { "a","b","c","d" }
You can use for-each and addAll():
for (ArrayList<String> subList : listOfLists) {
listofStrings.addAll(subList);
}
See:
https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
https://docs.oracle.com/javase/7/docs/api/java/util/List.html#addAll(java.util.Collection)
This question already has answers here:
How to add elements of a string array to a string array list?
(10 answers)
Closed 6 years ago.
I have 2 String Arrays
String[] question1 = {"Q1","Q2","Q3","Q4"};
String[] question2 = {"Q5","Q6","Q7","Q8"};
And one ArrayList
ArrayList<String> aList = new ArrayList<String>();
How can I manipulate them if i want to access to a member of ArrayList. I tried converting both arrays to String but it doesn't give solution.
ArrayList<String> aList = new ArrayList<String>(Arrays.asList(question1));
aList.addAll(Arrays.asList(question2));
This question already has answers here:
How does the Java 'for each' loop work?
(29 answers)
Closed 9 years ago.
I have a list of objects called employee() and I used emp as my object. I want to increment over employees and print the name of all the objects. I really lost on how to do this. Thanks.
for( emp ; ;employees())
{
System.out.println(emp.name);
}
Try using foreach()
for(Employee employee : employees())
{
System.out.println(employee.name);
}