• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Copying ListIterator

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am passing ListIterator to a function and making copies of the same in the function as I need to traverse thru the collection (ArrayList to be specific). But whne I traverse through the list all the ListIterator are getting updated.
Below is the sample code

public void calculateAllocationFactor(ListIterator iToDBList) {
ListIterator iPointer = iToDBList;
ListIterator iPointerBkp = iToDBList;
ListIterator iPointerSecBkp = iToDBList;
while (iPointer.hasNext()) {
//Some code
}
while (iPointerBkp.hasNext()){
//Some code
}
while (iPointerSecBkp.hasNext()) {
//Some Code
}

When I reach second while my iPointerBkp.hasNext() is returning false.
Can anyone please let me know if I have written really lousy code or if I have missed anyting.

Thanks,
Kashyap
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not copying the ListIterator at all. What you have is three different local reference variables that all point to the same ListIterator.
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ken,
Can you please let me know how to copy ListIterator as I CANNOT use

ListIterator iPointerBkp = new ListIterator();

Thanks for your reply.

-Kashyap
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You generally can't copy an iterator -- but that's OK, as I can give you three perfectly lovely alternatives:

1) Instead of asking the original List for one ListIterator, ask it for three, one at a time.

2) Roll all three tasks up into one big for loop.

3) Use the original ListIterator to make a new List -- i.e., as you iterate over the ListIterator, just store each element in new List. Then ask that new List for as many ListIterators as you want, in succession.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know that you can. ListIterator doesn't implement Cloneable and there's no facility that I see for copying one. Generally if you want another ListIterator you should get it from the List rather than trying to copy an existing one. What is it you hope to accomplish and why do you believe you need three copies of a ListIterator to do it?
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest and Ken.

I must loop thru the list once and calculate the average of one of the fields based on another field.
Eg.

If my structure is
{
field1
field2
field3
field4
}

I must add all field2 if field1 is same
say
{A,2,"Test1",Avg},
{A,3,"Test2",Avg},
{B,5,"Test3",Avg},
{B,7,"Test4",Avg}
Now I must calculate average of 2 and 3 as both contaiers have "A" as field1's value and put in corresponding structure having "A" in field1.
After looping I must have
{A,2,"Test1",2.5},
{A,3,"Test2",2.5},
{B,5,"Test3",6},
{B,7,"Test4",6}
Same way I have to calculate one more value on the Avg vaue field
Please let me know if I am confusing you!!

Thanks,
Kashyap
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use my option numbers 1 or 3.
 
reply
    Bookmark Topic Watch Topic
  • New Topic