• 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?
 
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
 
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?
 
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
 
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?
 
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?
 
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