ArrayList inside method / constructor - Java

After searching google, I could not find an answer for this, I am not very familiar with Java, I use C # most of the time, and I know that using C # is possible and probably it is in Java.

Ps: Sorry for the selection, I do not know how to use it here.

I have a constructor:

public WeaponsData(ArrayList<NPC> _drop, ArrayList<NPC> _buy, ArrayList<NPC> _sell) { } 

Then, when I try to create an Object that creates ArrayLists () directly on it, it does not work:

 public static WeaponsData AngelicAxe = new WeaponsData(new ArrayList<NPC>() { new NPC("Rat", "None", 0), new NPC("Dog", "None", 0) }, new ArrayList<NPC>() { new NPC("Player", "All", 0) }, new ArrayList<NPC>() { new NPC("Cain", "First", 5000) } ); 

There is no way to do this in Java?

thanks

+4
source share
8 answers

ArrayList does not have the constructors necessary for this. You can either wrap the arguments when calling Arrays.asList() :

 public static WeaponsData AngelicAxe = new WeaponsData( new ArrayList<NPC>( Arrays.asList( new NPC("Rat", "None", 0), new NPC("Dog", "None", 0) ) ), // etc ); 

or use the factory methods provided by the Guava Framework :

 public static WeaponsData AngelicAxe = new WeaponsData( Lists.newArrayList( new NPC("Rat", "None", 0), new NPC("Dog", "None", 0) ), // etc. ); 

Of course, if you use Guava, you should probably use an immutable collection, as you are apparently trying to implement a constant:

 public static final WeaponsData ANGELIC_AXE = new WeaponsData( ImmutableList.of( new NPC("Rat", "None", 0), new NPC("Dog", "None", 0) ), // etc. ); 
+6
source

If you use Eclipse Collections (formerly GS Collections ), you can use FastList to create a list. FastList implements java.util.List , as well as the richer types of GS special collections. It can be used as a replacement for ArrayList if you don't mind, if you don't support modCount. Your code will look like this using FastList .

 public static WeaponsData AngelicAxe = new WeaponsData( FastList.newListWith( new NPC("Rat", "None", 0), new NPC("Dog", "None", 0) ), // etc ); 

Note: I am a committer for Eclipse collections

+2
source

You need to call add on arraylist to add elements.

ArrayList does not have a constructor with a custom object.

Example:

 new ArrayList<NPC>().add( new NPC("Player", "All", 0)); 

EDIT: If add binding is required then Arrays.asList (..) needs to be used.

+1
source

Try the following:

 new ArrayList<NPC>(Arrays.asList(new NPC[] { new NPC("Rat", "None", 0), new NPC("Dog", "None", 0)})) 

-> creates an NPC array and displays a list from it

+1
source

The things you tried can be done for data arrays, but not for ArrayList

It works:

 new NPC[] {new NPC(...)}; 

It does not mean:

 new ArrayList<NPC>() {new NPC(...)}; 
0
source

Besides Arrays.asList (), you can also use double binding initialization :

 ArrayList<NPC> list = new ArrayList<NPC>() {{ add(new NPC("Rat", "None", 0)); add(new NPC("Dog", "None", 0)); }}; 
0
source

A couple of things style + flexibility:

 public WeaponsData(ArrayList<NPC> _drop, ArrayList<NPC> _buy, ArrayList<NPC> _sell) { } 

it should be:

 public WeaponsData(Collection<NPC> drops, Collection<NBC> buys, Collection<NPC> sells) {} 

To populate the Collection , use

  Collection.add(element); 

If you want to use the syntax { foo,bar,baz} , use an array. For instance:

 NPC[] npcs = new NPC[] {foo,bar,bax}; 

you can do Arrays.asList(npcs) to return it to List

0
source

Embedded initializers like this are not supported by default in Java. Java has something similar, but it's not exactly the same.

Here's how you can define an embedded ArrayList string (using a technique called “double-bracket initialization”):

 ArrayList<NPC> npcs = new ArrayList<NPC>() { { add(new NPC()); add(new NPC()); }}; 

As you can see, not very nice. You also actually instantiate the subclass of ArrayList instead of the ArrayList class.

Java typically uses the List<T> to define variables, so you can use any List implementation.

Consider this instead:

 public WeaponsData(List<NPC> _drop, List<NPC> _buy, List<NPC> _sell) { } 

Then you can do:

 public static WeaponsData AngelicAxe = new WeaponsData( Arrays.asList( new NPC("Rat", "None", 0), new NPC("Dog", "None", 0) ), Arrays.asList( new NPC("Player", "All", 0) ), Arrays.asList( new NPC("Cain", "First", 5000) ) ); 

There is also a set of Google Guava libraries that has a very good collection library with many helper methods that help in creating and processing collections:

Check it out: http://code.google.com/p/guava-libraries/ and User Guide

0
source

All Articles