aspose file tools
The moose likes Java in General and the fly likes How to give multiple iterator in Arraylist? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to give multiple iterator in Arraylist?" Watch "How to give multiple iterator in Arraylist?" New topic
Author

How to give multiple iterator in Arraylist?

Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
hi Team

i want to know that can i give an iterator to an arraylist, that starts from a dynamic location in the arraylist.

In my application, there is one arraylist, that contains a number of objects. i want to take each object and compare that object with the remaining objects in the arraylist. for example, take the first object, compare that with all the other objects, that start from second location. In the second iteration, i want to compare the second object with the objects starting from the third location.

Can i use a second iterator, that start in dynamic location(In first case 2nd location, then third location etc).

Thanks

Anoob


Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Yep, you can use ListIterators for that.

I have this exercise in one of my classes that does this. It is about a man walking in the woods.

Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
Thank You Ove Lindstr�m

thank you very much for your answer with example.


anoobkumar
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1409

Keep in mind that a ListIterator is a fail-fast iterator that throws a ConcurrentModificationException if it detects that the ArrayList is structurally modified by any means other than its own remove() or add() methods.
So if you request two ListIterator's for the same ArrayList, and one of them structurally modifies the ArrayList, it's no longer safe to continue iterating the ArrayList using the other ListIterator, as it will most likely throw a ConcurrentModificationException if you do.
[ August 27, 2008: Message edited by: Jelle Klap ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Hi,

Dont you think we should use the toArray method to get the array and then perform these comparsion. I think that would be easy

One of the purpose to add this method ( toArray()) is that some operations can be performed easly with arrays as compared to collections.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Although creating an array creates extra overhead (the array object, the time to fill it), you do have a point.

ArrayList implements RandomAccess, which means that you can simply use direct indexes to retrieve elements, just like an array. This should come as no surprise since ArrayList uses an array in the background

So you can just work with integers for your indexing instead of iterators. Although the iterator solution is of course useful for all types of lists, not just ArrayList.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
Actually is there any advantage to use arrays over Arraylist, other than the reduced complexity in usage of integer indexes?

will it adds the overhead of creating one array object, and also an array of objects?
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Just to add few points:-

About ArrayList:-

1) Boxing and Autoboxing are going to be take place during comparsion
2) Everytime we are calling functions provided in ListIterator

Both these point will have their own overhead...

About arrays:

1) As Rob Prime said, arrays have their own oberhead.

Rob Prime also said ArrayList is using arrays internally.
Now the question arise what is the purpose then to have toArray method in collection framework?

ofcourse one reason is to have backward compatability.

Do we have any other reason ?

Example : If ArrayList has Integer objects and we have to find the sum of all these objects
then I think first converting them to array first and then performing sum thru a loop is going to be the easy solution.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
Originally posted by bittoo garg:
Now the question arise what is the purpose then to have toArray method in collection framework?
The reason for the List#toArray method is simple: the designers thought users might want to convert their Lists to arrays. Why? Doesn't matter: that is for the user to decide.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Originally posted by bittoo garg:

Example : If ArrayList has Integer objects and we have to find the sum of all these objects
then I think first converting them to array first and then performing sum thru a loop is going to be the easy solution.

Two pieces of code which will do exactly the same thing, given an ArrayList<Integer> called integers:


The second one reads just a bit better I think.

Now if you want to improve afterwards: with 1000 iterations over a list of 1000 elements, the array solution is about twice as fast. However, with 30 ms, you will have to have really large lists if you want to notice a difference.
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
Hi Rob

As you said, if arrays used for large collections(of around 1000 objects), the looping time may be minimum. but it will create an overhead of creating an array of that much size. right?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
1000 isn't a large collection.
I don't think there is much overhead in terms of creating an array. You will have to try it, however, using the System class method which returns nanoseconds.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

There is overhead in the creation, but believe it or not, if I increase the size the array version will still be faster.

I've checked the API, and it couldn't possibly be the size() method - that just returns a private integer field.

The get method on the other hand always calls a private method called RangeCheck (way to follow your own style, Sun!) which checks if the upper bound is not exceed. So next to returning the array value it also performs that check, and apparently that is a bit of a performance hit.

Still, these performance gains are only worth the trouble if every nanosecond counts.


Oh and Campbell? I was using nanoseconds from the start
Milliseconds tend to be quite inaccurate for these kinds of measurements; differences of less than 20 ms are ignored most of the time.
[ August 28, 2008: Message edited by: Rob Prime ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to give multiple iterator in Arraylist?
 
Similar Threads
Struts2 checkboxes returning values in Action class
wrapper classes
gettting arraylist values using multithreading
Copying objects from one Arraylist to another Arraylist
Grouping objects by percentages