Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

How do I replace the following array with an ArrayList.

Employee[] companyTeam = {manager, engineer1, superviso1, accountant, intern };
share|improve this question

marked as duplicate by Jonathan, Steve Kuo, TheLittlePig, Alexander, qaphla Apr 4 at 3:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Arrays.asList() will help.. –  TheLostMind Jan 23 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. –  Jonathan Jan 23 at 14:50

2 Answers 2

You can do something like

List<Employee> companyTeam = Arrays.asList(manager, engineer1, superviso1, accountant, intern);
share|improve this answer
    
Arrays.asList(companyTeam) in case you already have an array.. –  TheLostMind Jan 23 at 14:43
    
the word "Arrays" and "List" is highlighted in red. Any idea why? –  Sameena Jan 23 at 14:51
    
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. –  Mike B Jan 23 at 14:57
    
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" –  Sameena Jan 23 at 15:16
    
You can do ArrayList<Employee> companyTeam = new ArrayList<Employee>(Arrays.asList(manager, engineer1, superviso1, accountant, intern)); –  Mike B Jan 23 at 15:29
 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

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.