File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList and order

 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Leandro Melo
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Leandro Melo
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys

AMD
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic