i cant find a way of naming my arrayLists so that when i search for 1 i can compare the names! if ive got something like this <-- havnt checked if the code compiles!
so class game creates a list, and gameList holds lists of Game objects (which are lists), but how can i name those lists that are of type Game? im honestly lost here any help is extremely appriciated Hannah [ April 05, 2005: Message edited by: H Melua ]
If you want a collection of named items, you might want to look into a collection that implements the Map interface. This allows you to enter an object into the collection with another object as it's key. For example,
There are also implementations of Map that provide for predicatable ordering, should you need that.
Have a read of the Java Collections Tutorial. There should be some useful info in there.
thanks i understand what ur saying, but we were told to use collections because we're not yet familiar with mapping very much! and it doesnt allow me to have dupicates, and i will have inorder to complete the tasks.
I think we need to back up here. Based on your original question, I think you might have a fundamental misunderstanding about ArrayLists and what they are for. So let me ask you a question: what do you mean exactly by "naming an arraylist", and why, exactly, do you need to do it? Try to explain in words, not code.
I agree with Ernest - your code looks *very* strange to me - I have no idea what it is supposed to do...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
lol ok, as far as i know arraylists hold objects...! but i need a way to give each collection of objects a name... e.g. Game class will have <----- the object in this class are not important! person1 person2 person3 all stored in 1 ArrayList
then it comes gameList (holds Lists) Game1 <------- this is an ArrayList Game2 <------- this is another arrayList Game3 <------- ....
so, i want to give Game1-3 different names so that whenever i need to search for them, i only need to give the name and it will look for it through GameList! is that any better?
the code is not complete by the way, class Person & GameList are omitted! Hannah
Having a little trouble understanding your question. Are you asking how to name your list? As in; ArrayList myList = new ArrayList(); where myList is the name? Then it seems like you're wanting to add a number of ArrayLists to a different list. This is where a map would come in handy, otherwise you'll have to access each list by it's index, not by it's name.
Edit: I was a little late on the trigger there. Looks like Ernest was thinking the same thing I was but was quicker to the punch. Oh and someone is eventually going to make you change your name, so might as well go ahead and do it, [ April 05, 2005: Message edited by: Hentay Duke ]
I find it strange that the Game class contains a list that then contains Game objects. Each of those objects will then also contain a list which contains more Game objects. And each of THOSE objects...do you see a problem here? Such self-referential data structures are not very common. Are you sure this ArrayList needs to be in the Game class? It seems to me that it should be stored somewhere external to the Game class.
thanks guys (Horatio Westock,Ernest Friedman-Hill,Hentay Duke,Layne Lund) im really grateful, although i cant use maps but ive learnt something at least.. ive solved it, instead i used hashset and included a name variable and played arround abit and got it to work... im feeling so happy
Hannah, you said that you need to allow duplicates, but the Set contract states that it does not allow duplicates.
Horatio, I think that by "it doesnt allow me to have dupicates, and i will have inorder to complete the tasks" Hannah meant that she must have duplicates so that it can complete the tasks, not that they need to be in sorted order.
true i do but for that bit of the code i dont as u know there are many lists... 1 will need to contain duplicates but the rest wont... i asked my lecturer if we are required to have only 1 type, and surprisingly HE ANSWERED ME "you'll need to use sets and lists" (full stop)
and as far as i know, hashMap is of type Map, and so its not a list or a set! unless im wrong as u may guess nothing is ever clarified at my college, u always need to struggle to know anything