Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Datastructures problem

Hi i need to find a datastructure that will store an array containing duplicate values, and whose size does not need to be preset, i have been using a hash table but it seems to my understanding that a hash table does not store duplicate values, right? I was thinking of using an array but dont know how to store an array in another array, they are both string arrays but i have been getting the error cant convert string to string, also i dont know the size of the array as it will continually change within the program i am writting. I was thinking of using vectors but really dont have much experience using them. Does anybody have any suggestions what might be best to use?
Thanks
[688 byte] By [cupanTae] at [2007-11-11 7:35:40]
# 1 Re: Datastructures problem
I think you're looking for an ArrayList ( http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html).
destin at 2007-11-11 22:37:51 >
# 2 Re: Datastructures problem
Excellent thanks
cupanTae at 2007-11-11 22:38:51 >
# 3 Re: Datastructures problem
http://javainterviewquestions.50webs.com/
I have compiled all possible questions. Feel free to suggest your own q/a.
vcinfy at 2007-11-11 22:40:00 >
# 4 Re: Datastructures problem
Thanks, that will be extremely helpful. I do however have one slightly more specific query which i could not find. What i have now stored in the array list is a list of match objects. I want to refer to each individual object in the arraylist, and would do that by using a for loop and an array[i] where i is the object when using an array, but dont know how to refer to an object in an arraylist. Does anyone know?
Thanks
cupanTae at 2007-11-11 22:41:00 >
# 5 Re: Datastructures problem
ArrayList has a method get( int index ). you can do:

ArrayList myArrayList = new ArrayList();

// put stuff in myArrayList

for ( int i = 0; i < myArrayList.size(); i++ ) {
System.out.println ( myArrayList.get( i ));
}

Note: the get method returns an Object, so in some cases you may find that you need to cast it (if you are casting to an int, float, etc. you need to use the classes Integer, Float, etc.)
destin at 2007-11-11 22:41:53 >
# 6 Re: Datastructures problem
It sounds like the storing of duplicate entries is important to you. You can "roll you own" structure, using hashcode as the mechanism to store and find values in the structure while allowing duplicate entries (it does not need to implement the Set or Map interfaces). You are not limited to the classes provided as a part of the Collections Framework.
nspils at 2007-11-11 22:43:03 >
# 7 Re: Datastructures problem
This thread has been busy!

What do you mean when you use the phrase that you don't "know how to refer to an object in an arraylist"?

If you need to have some "key" which allows you to put an "external identity" a particular object being stored in the arraylist, you need to use a Map (key, value) implementation - you look for the key and then extract the value associated with the key of interest. If you just want to iterate through the elements being stored in the arraylist, use an Iterator (or ListIterator). If you want to do something to each and every element in the list, use the extended for-each control (introduced in Java 1.5).

Also with Java 1.5, you can use generic identification to have the compiler check that all of the elements being stored in the ArrayList<datatype> is of a certain datatype - in that way, you aren't forced to use casting to establish the datatype of the Object being retrieved from the ArrayList.
nspils at 2007-11-11 22:44:01 >
# 8 Re: Datastructures problem
All the methods you'll need are in the link i gave you before. there's a get, a set, a remove, a removeRange, size, add, etc (there's even a contains method so you don't have to loop through each index to see if it has that object). Very useful class.
destin at 2007-11-11 22:44:59 >