• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2388
  • Last Modified:

Initialize a vector at declaration

I know for an Array u ca go: String myArray[] = {"a", "b", "c"}; ====> Initialization at declaration
How can i do a similar for a List? Exple: List myList = new ArrayList(  .....  ) or i am not sure...?
0
komlaaa
Asked:
komlaaa
2 Solutions
 
objectsCommented:
best you can do is:

List list = Arrays.asList(new String[] {"a", "b", "c"});
0
 
expertmbCommented:
>>Q:Initialize a vector at declaration
//Vector implements List
Vector v = new Vector(list);
0
 
MogalManicCommented:
Objects is right.  The only thing I can add is that the List returned by the Arrays object is a fixed size list.  To make the list dynamically sized:
  List list=new ArrayList(Arrays.asList(new String[] {"a", "b", "c"}));

This used the constructor that expertmb was refering to which copies one collection into another collection.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now