Datastructures problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Datastructures problem

  1. #1
    Join Date
    Oct 2005
    Posts
    16

    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

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    I think you're looking for an ArrayList.

  3. #3
    Join Date
    Oct 2005
    Posts
    16
    Excellent thanks

  4. #4
    Join Date
    Dec 2005
    Posts
    10
    http://javainterviewquestions.50webs.com/
    I have compiled all possible questions. Feel free to suggest your own q/a.

  5. #5
    Join Date
    Oct 2005
    Posts
    16
    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

  6. #6
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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.)

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  9. #9
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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

  1. Problem with Search
    By Irina in forum ASP.NET
    Replies: 0
    Last Post: 11-29-2002, 10:47 PM
  2. Replies: 0
    Last Post: 12-13-2001, 12:06 PM
  3. a problem with font and language
    By Roseta in forum VB Classic
    Replies: 0
    Last Post: 11-14-2001, 03:24 AM
  4. Arabic problem view
    By Ayman in forum VB Classic
    Replies: 0
    Last Post: 04-03-2000, 01:08 AM
  5. Problem with CryptoAPI and JCE
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center