d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Bonus Question On My Assignment Too Hard For Me. > Help Understanding Would Be Appreciated.
Prev12
Add Reply New Topic New Poll
Member
Posts: 29,367
Joined: Mar 27 2008
Gold: 504.69
Oct 25 2015 11:05am
Quote (carteblanche @ Oct 25 2015 01:00pm)
that doesnt look right. since you call previous(), next() should not be going forward all the time in your log. previous should be reseting it backwards some of the time. add logging for your previous. should be something like:

next() from 4 to 8
// previous sets it from 8 to 4
next() from 4 to 8


Weird so previous is not setting it back?
Quote
Detect Duplicates:
next() moved from null to 4
previous() moved from 4 to 4
There is a duplicate
next() moved from 4 to 4
next() moved from 4 to 8
previous() moved from 8 to 8
There is a duplicate
next() moved from 8 to 8
next() moved from 8 to 12
previous() moved from 12 to 12
There is a duplicate
next() moved from 12 to 12
next() moved from 12 to 16
previous() moved from 16 to 16
There is a duplicate
next() moved from 16 to 16
next() moved from 16 to 20
previous() moved from 20 to 20
There is a duplicate
next() moved from 20 to 20
Exception in thread "main" java.util.NoSuchElementException
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 25 2015 11:07am
what's your logging for previous?
Member
Posts: 29,367
Joined: Mar 27 2008
Gold: 504.69
Oct 25 2015 11:10am
Quote (carteblanche @ Oct 25 2015 01:07pm)
what's your logging for previous?


Copy paste of next

Code
public Object previous(){
Object previousValue = _source.previous();
System.out.println("previous() moved from " + _current + " to " + previousValue);
_current = previousValue;
return previousValue;

}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 25 2015 11:18am
Quote (ROM @ Oct 25 2015 01:10pm)
Copy paste of next

Code
public Object previous(){
Object previousValue = _source.previous();
System.out.println("previous() moved from " + _current + " to " + previousValue);
_current = previousValue;
return previousValue;

}

looks like it's just a notation issue.

Quote
its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next()


so if this is your list:
4, 8, 12
here's your cursor per call, line by line, for one iteration of your loop:

cursor, 4, 8, 12
call next: returns 4
4, cursor, 8, 12
call previous: returns 4
cursor, 4, 8, 12
call next: returns 4
4, cursor, 8, 12

so when you call next() you pass a number and return it. when you call previous, you jump right back and return the same number. hence it thinks it's a duplicate. so what you're thinking of as previous in your mind isn't the same as previous in the list. so adjust your code to look at the element you want to look at. keep in mind if you call previous multiple times, you may need to compensate with extra next() to get the cursor where you want. i suggest drawing out what i drew out for your example, by hand. then after you get your trace done, then write your code.

This post was edited by carteblanche on Oct 25 2015 11:23am
Member
Posts: 29,367
Joined: Mar 27 2008
Gold: 504.69
Oct 25 2015 11:50am
Some improvement.
Move the cursor forward twice, and compare the two numbers returned, then move back one.

Code
while(recordIterator.hasNext())
{
if(recordIterator.hasPrevious())
{
if(recordIterator.next().equals(recordIterator.next()))
{
System.out.println("There is a duplicate");
}
else
{
System.out.println("There are no duplicates");
}
}

recordIterator.previous();
}

Quote
Detect Duplicates:
next() moved from null to 4
next() moved from 4 to 8
There are no duplicates
previous() moved from 8 to 8
next() moved from 8 to 8
next() moved from 8 to 12
There are no duplicates
previous() moved from 12 to 12
next() moved from 12 to 12
next() moved from 12 to 16
There are no duplicates
previous() moved from 16 to 16
next() moved from 16 to 16
next() moved from 16 to 20
There are no duplicates
previous() moved from 20 to 20
next() moved from 20 to 20
Exception in thread "main" java.util.NoSuchElementException


But it moves from after 20 to before, so there is a next number but it can't move forward twice.
Member
Posts: 29,367
Joined: Mar 27 2008
Gold: 504.69
Oct 26 2015 05:15am
Update. Got it to go through the list properly but its not testing equality properly.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll