Java Array of Arraylists

Try changing
Code:
System.out.println(al[0].get(0).fname());

to
Code:
System.out.println(((Actor)al[0].get(0)).fname());

its pulling out an object from your arraylist, you need to tell it that its of class actor... then it will work.
 
Last edited:
^^^ Not quite, It should be something like this

Actor Class
Code:
public class Actor {
   private String fname, lname;
   public Actor(String fname,String lname) {
      this.fname = fname;
      this.lname = lname;
   }
   public String getFname() {
      return this.fname;
   }
   public String getLname() {
      return this.lname;
   }
}

TestActor class
Code:
import java.util.ArrayList;
public class TestActor {
   public static void main(String [] args) {
       ArrayList a1[] = new ArrayList[3];
       a1[0] = new ArrayList<Actor>();
       a1[1] = new ArrayList<Actor>();
       a1[2] = new ArrayList<Actor>();
       a1[0].add(new Actor("Sylvester","Stallone"));
       System.out.println(((Actor)a1[0].get(0)).getFname());
   }
}

This works, I've just tested it myself. Also the warning you're getting is because adding to or getting from an ArrayList can produce an IndexOutOfBoundsException, you'll either need to state that the method throws this exception or try to catch it with a try-catch block
 
Last edited:
Also the warning you're getting is because adding to or getting from an ArrayList can produce an IndexOutOfBoundsException, you'll either need to state that the method throws this exception or try to catch it with a try-catch block

IndexOutOfBoundsException is unchecked, the compiler won't generate warnings about it.

The warning is being generated because you're adding an Actor to an untyped ArrayList.

Arrays of generified collections are a bit funny in java, as you can see you're having to cast back to Actor objects when getting one from one of the ArrayLists, all a bit :(

You can get around this by having an arraylist of arraylists of actors so
ArrayList<ArrayList<Actor>> mActorLists = new ArrayList<ArrayList<Actor>>();
then mActorLists.add(new ArrayList<Actor>());

Will prevent casts and generally be nicer.

Code:
  1 import java.util.ArrayList;
  2 
  3 public class Actor {
  4    private String fname, lname;
  5    public Actor(String fname,String lname) {
  6       this.fname = fname;
  7       this.lname = lname;
  8    }
  9    public String getFname() {
 10       return this.fname;
 11    }
 12    public String getLname() {
 13       return this.lname;
 14    }
 15 
 16         public static void main(String[] args){
 17                 ArrayList<ArrayList<Actor>> mActors = new ArrayList<ArrayList<Actor>>();
 18                 mActors.add(new ArrayList<Actor>());
 19                 mActors.get(0).add(new Actor("Tom", "Jones"));
 20 
 21                 System.out.println(mActors.get(0).get(0).getFname());
 22         }
 23 }

Prints out tom as expected, no warnings, everyone's a winner.
 
Ahhh ok I did try catching the index out of bounds afterwards and was still getting the same thing. I did ponder using an arraylist of arraylists but decided to keep with the original example RobMunfy was using.

Also Tom Jones isn't an Actor ;)
 
^^^ Not quite, It should be something like this

Not sure what your on about, the above code that I have stated does work...

Your actor class is identical in use, except you added the return method for lname as well.

Thats the only difference in your code, that and your style(dont need use of this. in my case but same result, no negative effect with either).

I would generally write the method in the form you stated, however I tried to leave it as clear as possible to understand.

And nice one Dfhaii for bringing that up, slipped my mind...
 
Last edited:
I marvel at these problems strict typing produces. Well done type strict type languages for causing so much headache. :D

/pat Smalltalk
 
Not sure what your on about, the above code that I have stated does work...

Your actor class is identical in use, except you added the return method for lname as well.

Thats the only difference in your code, that and your style(dont need use of this. in my case but same result, no negative effect with either).

I would generally write the method in the form you stated, however I tried to leave it as clear as possible to understand.

And nice one Dfhaii for bringing that up, slipped my mind...

Actually I dunno if it was a typo but you had your cast as <Actor> instead of (Actor), minor I know.

Damn you've edited it, but you did have the following

System.out.println((<Actor>al[0].get(0)).fname());
 
Back
Top Bottom