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 ]
Horatio Westock
Ranch Hand
Joined: Feb 23, 2005
Posts: 221
posted
0
Hello,
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.
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.
thanks for that anyway Hannah
Horatio Westock
Ranch Hand
Joined: Feb 23, 2005
Posts: 221
posted
0
and it doesnt allow me to have dupicates
Does it make sense to have duplicate games? If so, then there are implementations which allow one key to map to a collection of items.
and i will have inorder to complete the tasks.
If you have a peek at the collections tutorial, you will see that there are implementations of Set and Map that have either sorting or predicatable iteration order.
Of course, as with everything, you could write your own! You'd need one collection to store your game objects, and another list to store information about the name and index for each game.
Good luck.
Ernest Friedman-Hill
author and iconoclast
Marshal
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
H Melua
Ranch Hand
Joined: Jan 04, 2005
Posts: 168
posted
0
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
Ernest Friedman-Hill
author and iconoclast
Marshal
OK, I understand what you want to do -- although not well enough to recommend an alternative. Almost all the time when you think of making a lists of lists of lists, there's a better solution.
The suggestion to use a Map was a good one, then. The names should be the keys, and the ArrayLists should be the values.
Hentay Duke
Ranch Hand
Joined: Oct 27, 2004
Posts: 198
posted
0
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 ]
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
To learn more about Collections, you should work through the Collections trail in the Java Tutorial. This should give you a good idea about what Collections are available and the strengths and weaknesses of each.
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.
Layne
H Melua
Ranch Hand
Joined: Jan 04, 2005
Posts: 168
posted
0
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
thanks again for ur help Hannah
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
posted
0
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.
Christina Smith
Greenhorn
Joined: Apr 05, 2005
Posts: 12
posted
0
....
emmm roo7i egri ktaabek ba3dean t3aali, 5ebla maate3refi t3'eshi [ April 06, 2005: Message edited by: Christina Smith ]
H Melua
Ranch Hand
Joined: Jan 04, 2005
Posts: 168
posted
0
hey nice to c u (david, chris)
looooool leave me alone... (chris)
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