Initialize a vector at declaration

Posted on 2004-11-04
Last Modified: 2006-11-17
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...?
Question by:komlaaa
    LVL 92

    Assisted Solution

    best you can do is:

    List list = Arrays.asList(new String[] {"a", "b", "c"});
    LVL 6

    Expert Comment

    >>Q:Initialize a vector at declaration
    //Vector implements List
    Vector v = new Vector(list);
    LVL 21

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Suggested Solutions

    Title # Comments Views Activity
    VB Script 3 60
    scoresSpecial  challenge 13 34
    Basics of Drool Rule Engine with few samples 2 31
    Modeling a class in java 5 22
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now