| Author |
ArrayList and order
|
Andrew Mcmurray
Ranch Hand
Joined: Sep 24, 2005
Posts: 188
|
|
Hi all, Does an ArrayList guarantee order with its adds. For example if I have two lists and I say list1.add(obj1), and list2.add("description of obj1") can I be sure that there is a one to one mapping between the two list based on index? Thanks, AMD
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Well... Yes, but you're on thin ice with this approach. You would need to ensure that everything is done in parallel (when you add to list1, you add to list2; when you remove from list1, you remove from list2; etc.) and nothing can interfere with this or your lists will be out of sync. You might consider using a Map (that stores pairs) instead of trying to maintain two Lists in tandem. Another option might be to define a "wrapper" class that holds the object and a description of the object. Then just put wrapper instances in a single List. [ May 24, 2007: Message edited by: marc weber ]
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Leandro Melo
Ranch Hand
Joined: Mar 27, 2004
Posts: 401
|
|
Hi. I suggest you to follow marcs idea on having a wrapper class that contains the object and itd description. The disadvantage of using a map is that you'll switch from constant time insertion to logarithmic time insertion. But depending on how you operate this list, it might be a good idea. I'd be necessary more knowledge of your requirements to make an opinion.
|
Leandro Melo <br />SCJP 1.4, SCWCD 1.4<br /><a href="http://www.pazbrasil.org/" target="_blank" rel="nofollow">http://www.pazbrasil.org/</a>
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[Leandro]: The disadvantage of using a map is that you'll switch from constant time insertion to logarithmic time insertion. I believe you're thinking of a TreeMap. But a LinkedHashMap would maintain the mappings in insertion order, with constant-time insertion (on average). I think a bigger issue is, is there a natural need for a key-value relationship between the two different objects being stored? Do you need to look up obj2 using obj1? If there is, then use a Map; if not, then using a Map seems needlessly confusing, and using a List of some WrapperObject instances seems cleaner. [ May 24, 2007: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Leandro Melo
Ranch Hand
Joined: Mar 27, 2004
Posts: 401
|
|
Hi Jim.
Originally posted by Jim Yingst: I believe you're thinking of a TreeMap.
Yes, I thought of a tree... I've been using std::map (C++) too much and it's usually implemented as a red-black tree . But you're correct (as usual), the hash based data structures provide amortized constant time insertion. [ May 25, 2007: Message edited by: Leandro Melo ]
|
 |
Andrew Mcmurray
Ranch Hand
Joined: Sep 24, 2005
Posts: 188
|
|
Thanks guys AMD
|
 |
 |
|
|
subject: ArrayList and order
|
|
|