looking ahead in the list with iterator
Hello
I am trying to look at the NEXT (not current) element in the list.
so i am doing something like this
while (dt.hasNext()&& i<5 && num!=nextnum)
{
DateUIObject srvdt = (DateUIObject) dt.next();
num = srvdt.getPosition();
nextnum = ((DateUIObject) dt.next()).getPosition();//here i want nextnum to have NEXT elements getPosition
i++;
}
so if i do this. the statement where i am initializing nextnum...i am losing one element of the list (since i did dt.next() the pointer has moved.) is there a way to move the pointer back one maybe?
All i am trying to do it i want to keep iterating thru the list only if there are elements in the list, dt.hasNext(), or if i is less than 5, i < 5, or if the number i get from current position is NOT SAME as the number i will get from next position.
kinda confusing.
is there a way to SET the iterator point to a specific index in the list?
# 1
no reason to move the pointer back and forth..DateUIObject nextObject = (DateUIObject) dt.next();
while (dt.hasNext()&& i<5 && num!=nextnum)
{
DateUIObject srvdt = nextObject;
num = srvdt.getPosition();
nextObject = ((DateUIObject) dt.next());
nextnum = nextObject.getPosition();
i++;
}
"i" may be off by one compared to your original code
# 3
> no reason to move the pointer back and
> forth..DateUIObject nextObject = (DateUIObject)
> dt.next();
>
> while (dt.hasNext()&& i<5 && num!=nextnum)
> {
> DateUIObject srvdt = nextObject;
> num = srvdt.getPosition();
> nextObject = ((DateUIObject) dt.next());
> nextnum = nextObject.getPosition();
> i++;
> }
"i" may be off by one compared to your
> original code
That helped a lot but i guess i should have given more away as a question. but i thought solving this would solve my problem.
Actually there is another while loop in which this while loop runs.
so for example (by the way this is for a site)
two Rows A and B each have 4 different columns (date columns)
elements for two rows (A and B) are stored in a List and then the dates are stored in ANOTHER list.
so the outer while is for the two rows and inner while (above) is for the dates. Now the problem is that. imagine a case where the list for rows has just two elements in it (A and B). and for the 4 columns of date the Date List only has two elements. one for A and another for B.
If i use the code above. it does do just one iteration (dates for A) and then checks if the position of NEXT element is also same. if it is. it stops the iteration which is fine. but then it goes back to the outer while and sees there is another iteration left for that while. comes back in outer while (for item B). chekcs if (dt.hasNext()). it doesnt because when we did .next() to determine position of next position that 'second' element was lost. so in iteration of B the second element in the List date is lost. and nothing is printed for dates of B.
sorry..lotta writiing. let me try make an examples
ListIterator dt = dates.listIterator();
ListIterator rows = elements.listIterator();
while (rows.hasNext())
{
String row = (String)rows.next(); //A or B for the example
int i = 1;
if (dt.hasNext())
DateUIObject nextObject = (DateUIObject) dt.next();
while (dt.hasNext() && i <5 && num!=nextnum)
{
DateUIObject srvdt = nextObject;
System.out.println(srvdt.getStart() + " " + srvdt.getEnd());
num = srvdt.getPosition();
nextObject = ((DateUIObject) dt.next());
nextnum = nextObject.getPosition();
i++;
}
for (; i<5; i++)
{
System.out.println("N/A");
}
}
so u see. when i go back to the rows.hasNext() loop (it still has B in it). i do if(dt.hasNext()) it doesnt have anything in it and hence the date for the B row is lost. :(
# 4
You missed my point with the ListIterator. A ListIterator allows you to go backwards using "previous". So, if "next" doesn't give what you want, you can back up.
You can also check the "nextIndex" (or previous index) and get the value of the element at that index without actually advancing the iterator.
Depending on what type of object "dates" is, it would look something like this:
nextObject = ((DateUIObject) dt.next());
to
nextObject = (DateUIObject)dates.get(dt.nextIndex());