8

How do I replace the following array with an ArrayList.

Employee[] companyTeam = {manager, engineer1, superviso1, accountant, intern };
2
  • Arrays.asList() will help.. Commented Jan 23, 2014 at 14:41
  • If you really need a java.util.ArrayList check the duplicate question (above). FYI, Arrays.asList really returns an Arrays.ArrayList, although that might not matter. Commented Jan 23, 2014 at 14:50

2 Answers 2

4

You can do something like

List<Employee> companyTeam = Arrays.asList(manager, engineer1, superviso1, accountant, intern);
Sign up to request clarification or add additional context in comments.

5 Comments

Arrays.asList(companyTeam) in case you already have an array..
the word "Arrays" and "List" is highlighted in red. Any idea why?
You need to import them. Add import java.util.List; and import java.util.Arrays; to the top of your file. Or if you're using Eclipse ctrl + shift + O will import them for you.
Hi Mike, that works. Thanks. But if Im only to use the import java.util.ArrayList command. How would I go about writing the code for it? I tried using ArrayList<Employee> companyTeam = {manager, engineer1, superviso1, accountant, intern};but then im getting an "illegal initializer for for ArrayList error"
You can do ArrayList<Employee> companyTeam = new ArrayList<Employee>(Arrays.asList(manager, engineer1, superviso1, accountant, intern));
0
 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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.