35

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

ssbl
  • 43
  • 2
  • 6
Ngo Ky
  • 415
  • 1
  • 4
  • 11

6 Answers6

55

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

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • This should really be List in the declaration – RNJ Oct 01 '12 at 09:21
  • This is fun but in what is it better or simpler than just doing `list.add(new...` just after `List list = new ArrayList()` ? – Denys Séguret Oct 01 '12 at 09:26
  • does it need constructor of myclass like: myclass(string, string) in myclass? – Ngo Ky Oct 01 '12 at 09:28
  • @dystroy.. I have given a link to a post on SO, that describes everything about its pros and cons.. Just click on that link.. – Rohit Jain Oct 01 '12 at 09:28
  • @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.. – Rohit Jain Oct 01 '12 at 09:29
  • But note, as the other post mentions, that this might introduce memory leaks. It creates a new anonymous inner class, which holds a reference to the containing object. Hence, if you have some reference to the list somewhere, without having an explicit reference to the containing object, the containing object still can't be garbage collected. – Tohnmeister Dec 05 '19 at 12:35
29

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
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) {
...
}
Dan D.
  • 32,246
  • 5
  • 63
  • 79
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. :)

David Grant
  • 13,929
  • 3
  • 57
  • 63
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;
}
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
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.

Community
  • 1
  • 1
Segmented
  • 2,024
  • 2
  • 23
  • 44