38

i have MyClass as

MyClass(String, String, int);

i know about how to add to add to ArrayList in this way:

MyClass.name = "Name";
MyClass.address = "adress";adress
MyClass.age = age;

then add to arrayList like:

list.add(MyClass);

but now i have many object MyClass in static form, i want to add

ArrayList<MyClass> list = new ArrayList<MyClass>({"Name","Address", age};.....);

can i do like this. thank anyway

1
  • 6
    Classes in Java should be named like MyClass. Commented Oct 1, 2012 at 9:13

6 Answers 6

56

You can use double braces initialization: -

List<MyClass> list = new ArrayList<MyClass>() {
    {
        add(new MyClass("name", "address", 23));
        add(new MyClass("name2", "address2", 45));
    }
};

As you can see that, inner braces is just like an initializer block, which is used to initialize the list in one go..

Also note the semi-colon at the end of your double-braces

Sign up to request clarification or add additional context in comments.

6 Comments

This should really be List<MyClass> in the declaration
This is fun but in what is it better or simpler than just doing list.add(new... just after List<MyClass> list = new ArrayList<MyClass>() ?
does it need constructor of myclass like: myclass(string, string) in myclass?
@dystroy.. I have given a link to a post on SO, that describes everything about its pros and cons.. Just click on that link..
@NgoKy.. Well you can have any kind of constructor, and use it in this block.. That doesn't really matter.. But, ideally you should need to have at least a 1-arg constructor so that you can initialize your object here only (that is what you want).. Else you will need to iterate over your list to initialize Object's attribute..
|
32

You can do

List<MyClass> list = Arrays.asList(
                         new MyClass("Name", "Address", age),
                         // many more
                     );

Note: this will create a list where you can't change its size.

1 Comment

This works nicely for defining a constant (static final) array as a class member.
8

Something like this:

List<MyClass> list = new ArrayList<MyClass>(Arrays.asList(new MyClass[] {new MyClass("Name", "Address", age}));

Of course, your class must have a constructor like this:

public MyClass(String name, String address, int age) {
...
}

Comments

3

You can instantiate ArrayList like so:

new ArrayList<myclass>() {{
  add(new MyClass("Name", "Address", age));
}};

This creates an anonymous inner class that actually extends ArrayList, with an initialiser block that calls add. This is obviously completely filthy and will make your colleagues want to hurt you, so you should use Arrays.asList instead. :)

Comments

1

If what you want is having the cleanest and simplest initialization, you might have this :

    List<MyClass> list = MyClass.build(
        "Name", "Address", 51,  
        "a2", "c", 4,
        "v", "d", 2
    );

This uses this utility method :

public static List<MyClass> build(Object... array) {
    List<MyClass> list = new ArrayList<Test>();
    for (int i=0; i<array.length-2; i+=3) {
        list.add(new MyClass((String)array[i], (String)array[i+1], (Integer)array[i+2]));
    }
    return list;
}

Comments

0

The "best" way to do this in an effective way would be to :

List<MyClass> list = new ArrayList<MyClass>();

 list.add(new MyClass("name", "address", 23));
 list.add(new MyClass("name2", "address2", 45));

although it requires a lot of typing but as you can clearly see this is more efficient

Another alternative would be to use google guava (not tested for efficiency):

ArrayList<MyClass> list = new ArrayList<MyClass>(
           new MyClass("name", "address", 23), 
           new MyClass("name2", "address2", 45) );

The required import is import static com.google.common.collect.Lists.newArrayList;

also, you can use double braces initialization as originally proposed by @Rohit Jain: -

List<MyClass> list = new ArrayList<MyClass>() {
    {
        add(new MyClass("name", "address", 23));
        add(new MyClass("name2", "address2", 45));
    }
};

As you can see that, inner braces is just like an initializer block, which is used to initialize the list in one go..

note the semi-colon at the end of your double-braces

also note the last method has some downsides as discussed here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.