• Notice regarding image hosts and attachments. See here.

Java Array of Arraylists

Associate
Joined
18 Mar 2007
Posts
291
Hi guys,

How do you create an array of length n of arraylists in java? What would the syntax look like? I've tried:

Code:
ArrayList<>[] al = new Arraylist[3];

but it doesn't seem to like it (Illegal start of type).

Any tips?
 
Code:
ArrayList<>[] al = new Arraylist[3];

its your <> it doesnt like

You need to have <String> or something like below


if you have various data types in the array just use
Code:
ArrayList[] al = new Arraylist[3]();

Or like you had if you want a string say use
Code:
ArrayList<String>[] al = new Arraylist<String>[3]();
 
ah thanks a lot.

if i have created a new data type called Actor that takes two strings and did:

Code:
ArrayList<Actor>[] al = new Arraylist<Actor>[3]();

to then add an element in, for example, arraylist no 6, would i do:

Code:
al[6].add("Sylvester","Stallone");
?

Thanks
 
Nope, you need to create an Actor object and pass that into the add method. Also, the 6th element would be at array index 5, since Java's arrays are zero-based.

Code:
al[5].add(new Actor("Sylvester", "Stallone"));
 
I'm getting an error from:

Code:
ArrayList<String>[] al = new ArrayList<String>[3]();

saying:

Code:
';' expected
                  ArrayList<String>[] al = new ArrayList<String>[3]();
                                                                   ^

Any ideas?
 
When you initialise an array, you don't need to put the empty parentheses after the square brackets. For example, when initialising an array of integers, you'd do this:

Code:
int[] array = new int[3];

It's exactly the same with array lists; you specify the type of the array (in this case, ArrayList<String>) followed by square brackets specifying the length of the array:

Code:
ArrayList<String>[] al = new ArrayList<String>[3];

Remember, though, that all of the array's elements will start off as just null (since ArrayList<String> is a reference type), so you need to initialise each element of the array with a new array list:

Code:
al[0] = new ArrayList<String>();
al[1] = new ArrayList<String>();
al[2] = new ArrayList<String>();

Hope that makes sense :)
 
Last edited:
Doesn't this create an array (with 3 elements) of ArrayLists with an initial capacity of ten?

Code:
ArrayList<String>[] al = new ArrayList<String>[3]();

Here are the constructors allowed

Code:
ArrayList

public ArrayList(int initialCapacity)

    Constructs an empty list with the specified initial capacity.

Parameters:
    initialCapacity - the initial capacity of the list. 
Throws:
    IllegalArgumentException - if the specified initial capacity is negative

ArrayList

public ArrayList()

    Constructs an empty list with an initial capacity of ten.

ArrayList

public ArrayList(Collection c)

    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. The ArrayList instance has an initial capacity of 110% the size of the specified collection.

Parameters:
    c - the collection whose elements are to be placed into this list.

Edit: Actually I see, so yes it will create an array of ArrayLists but as Inquistor said you'll need to initialise them, duhhhhhh! <---- that's aimed at me
 
Last edited:
Hey,

I'm still getting an error with:

Code:
ArrayList<String>[] al = new ArrayList<String>[3];
al[0] = new ArrayList<String>();
al[1] = new ArrayList<String>();
al[2] = new ArrayList<String>();

saying:
Code:
generic array creation
                  ArrayList<String>[] al = new ArrayList<String>[3];
                                           ^

Thanks for your help so far though Inquisitor
 
Try this instead

Code:
           ArrayList a1[] = new ArrayList[3];
           a1[0] = new ArrayList<String>();
           a1[1] = new ArrayList<String>();
           a1[2] = new ArrayList<String>();

This will create an array of ArrayLists each of which is limited to holding objects of type String
 
ok guys,

What i've done now is done:

Code:
ArrayList[] al = new ArrayList[3];
al[0]=new ArrayList<Actor>();
al[1]=new ArrayList<Actor>();
al[2]=new ArrayList<Actor>();
al[0].add(new Actor("Sylvester","Stalone"));
^^^^^^Produces a warning^^^^^^^^^

How would I then go about printing out the firstname (Called fname in the Actor class) that I have just added?
 
System.out.println(al[0].get(0).fname);

if fname is protected / public. if you have a accessor method in the actor class replace fname with the method to get fname.
 
Code:
System.out.println(al[0].get(0).fname);

Compiler is saying that it cannot find the symbol fname.

my actor class looks like this:

Code:
class Actor {
	
	public String fname;
	public String lname;
	
	public Actor(String x, String y) {
		fname=x;
		lname=y;
	}
}

Is there anything wrong here?
 
Also, the warning says:

Code:
warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                                        al[0].add(new Actor("Sylvester","Stallone");
                                                 ^

What's the warning about?
 
You need to create a method in Actor to return what you need, e.g following should fix:-
Code:
class Actor {
	
	public String fname;
	public String lname;
	
	public Actor(String x, String y) {
		fname=x;
		lname=y;
	}
        
        public String fname(){
               return fname;
        }
}

thus
Code:
System.out.println(al[0].get(0).fname());
should then pull back the fname in that location.
 
ok, my code looks like this:

Code:
ArrayList[] al = new ArrayList[3];
al[0]=new ArrayList<Actor>();
al[1]=new ArrayList<Actor>();
al[2]=new ArrayList<Actor>();
al[0].add(new Actor("Sylvester","Stalone"));
System.out.println(al[0].get(0).fname());

and the Actor class looks as it does in your bit of code:
Code:
class Actor {
	
	public String fname;
	public String lname;
	
	public Actor(String x, String y) {
		fname=x;
		lname=y;
	}
        
        public String fname(){
               return fname;
        }
}

I am compiling with:

Code:
javac MyArrayListExample.java Actor.java

The errors are:
Code:
cannot find symbol
symbol  : method fname()
location: class java.lang.Object
                                        System.out.println(al[0].get(0).fname());

                                                                       ^
1 error

and

Code:
warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                                        al[0].add(new Actor("Sylvester","Stalone"));
                                                 ^
1 warning

I think maybe I am adding the Actor to the ArrayList in the wrong way. Still doesn't explain why it "cannot find symbol" though.

Thanks a lot for your help
 
Back
Top Bottom