Results 1 to 4 of 4
  1. #1
    TSTONESIX is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Multidimensional ArrayList Help

    So I am working with an ArrayList of an object, which also contains within it an ArrayList of String values. I want to add an item to the String values for a specific object in my ArrayList of objects. What is an efficient way to do this? Should I use indexOf() to get the placement of the object and then try to set a value for that object? I am also working with someone elses code, and do not have the option of putting a setter in the class that contains the ArrayList of Strings.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Multidimensional ArrayList Help

    Why are you using this data structure to begin with because it smells *very* fragile, and I'm almost certain that there's a better way to go about solving your ultimate goal. And so I suggest that you tell us just this: what is your ultimate goal with this code and data structure?

  3. #3
    TSTONESIX is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Multidimensional ArrayList Help

    In a basic form, let's say I am putting together a database that keeps track of television shows. I am keeping an ArrayList of an Object called Show. Each Show contains a String value for the name of the show, and an ArrayList of strings, each of which is an actor's name on the television show. One of the shows adds a new character, and I want to add a new actor to the list of names for the given show. How can I add this String to the ArrayList of Strings in my ArrayList of shows using .add(String) ? I am thinking something along the lines of this, but I am not sure how to implement adding an object to an ArrayList within another ArrayList.

    Java Code:
    public class Show() { //this is the basic object class that I am supposed to use. It has a basic constructor with 
    //null values, and I can use getTitle() to get a String value, and getCast() to return a List<String> value
    String title;
    List<String> cast;
    
    }
    
    pulbic class Shows() //this is a class that I am to complete, which is supposed to have an option to add a cast member to one of the shows. 
    //I am unsure how to do this. 
    Show<ArrayList> shows;
    
    public null addCastMember(String title, String name){
    shows.getTitle(title).cast.add(name);  //this is in a sense, what I am trying to do, but am not sure how to do correctly
    }
    Last edited by TSTONESIX; 02-26-2013 at 10:35 PM.

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Multidimensional ArrayList Help

    I would suggest using a HashMap<String, Show> instead (or something similar). That way you can easily grab a Show by a unique identifier (its title), and then do something like

    map.get("Cosby Show").addCastMember("Bill Cosby");

    Or, perhaps extend HashMap<String, Show> and add that method, which uses the above line of code (using "this" instead of "map").

    Java Code:
    public class ShowMap extends HashMap<String, Show>
    {
    	public void addCastMember(String title, String name)
    	{
    		this.get(title).addCastMember(name);
    	}
    }
    Last edited by AndrewM16921; 02-27-2013 at 06:56 AM.

Similar Threads

  1. Multidimensional arrys
    By shavindra in forum New To Java
    Replies: 5
    Last Post: 12-14-2012, 02:52 PM
  2. Multidimensional Array
    By TyCox94 in forum New To Java
    Replies: 11
    Last Post: 09-25-2011, 11:00 PM
  3. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  4. Multidimensional Arrays
    By holytanx in forum New To Java
    Replies: 1
    Last Post: 08-05-2010, 10:28 PM
  5. Multidimensional hashtable?
    By jklsemicolon in forum New To Java
    Replies: 6
    Last Post: 08-17-2008, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •