Newbie arraylist question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Newbie arraylist question

  1. #1
    Join Date
    Mar 2004
    Posts
    6

    Newbie arraylist question

    This is a stupid newbie question!

    I have 2 arraylists which are linked. One arraylist contains values 1-30 and the other contains a short "memo".

    how do i go about displaying all of them in the terminal window so that when a number is selected from arraylist 1 the corresponding value is selected from arraylist2.

    Can i sort arraylist1 into ascending order so that when the values are displayedit would be like...1, 2, ...

    Thank you for your time

    Carl

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    Hi Carl,

    It sounds like data structure wise, you probably don't need/want two ArrayLists here. There are always a few different options when it comes to how you set up your data structures so I'll suggest one or two.

    I'm guessing you are storing the 1-30 values as Integer classes since ArrayList takes objects. You could use a single java.util.HashMap instead since it holds key/value pairs. Your HashMap's key is your 1-30, and the HashMap's value is the "memo" (assuming a String). Once you have your HashMap initialized, getting the correpsonding value to the user-chosen 1-30, let's say 10, is as easy is
    Code:
    myHashMap.get(new Integer(10))
    Now, I'm assuming the 1-30 is not just an index, that it has some meaning and the order is not necessarily always guaranteed to be 1,2,3 etc... If it's really just an index, then you should be able to use a single ArrayList and let the Lists's index serve as the 1-30. So you could create a new ArrayList of size 30
    Code:
    myArrayList = new ArrayList(30)
    and use that. If they choose 10, access the value with
    Code:
    myArrayList.get(10-1)
    I used 10-1 because your index will be from 0-29 and you're presenting the user with 1-30.

    Hope that makes some sense? Let me know if I'm way off and I'll try again.
    -- Steven

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