-
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
-
I think you're looking for an ArrayList.
-
-
http://javainterviewquestions.50webs.com/
I have compiled all possible questions. Feel free to suggest your own q/a.
-
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
-
ArrayList has a method get( int index ). you can do:
Code:
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.)
-
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.
-
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.
Last edited by nspils; 12-27-2005 at 08:03 PM.
-
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.
Similar Threads
-
By Irina in forum ASP.NET
Replies: 0
Last Post: 11-29-2002, 10:47 PM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
By Roseta in forum VB Classic
Replies: 0
Last Post: 11-14-2001, 03:24 AM
-
By Ayman in forum VB Classic
Replies: 0
Last Post: 04-03-2000, 01:08 AM
-
By Jason Bock in forum VB Classic
Replies: 0
Last Post: 03-21-2000, 06:48 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks