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.
12Next
Add Reply New Topic New Poll
Member
Posts: 29,350
Joined: Mar 27 2008
Gold: 504.69
Oct 25 2015 08:54am
The purpose of the assignment was to learn about Linked Lists and List Iterator.

In the first part of the assignment we create an array of strings, make it into a Linked List, display it as a queue/stack, make the Linked List into a List Iterator, display it as a queue/stack, sort the Linked List, then prompt the user to delete elements from the list.

In the second part we create an array of integers, display the array using an overridden toString() method, sort the array using the Arrays class, load that array into a Linked List, and then sum the total of the elements in the link list.

In order to use the Arrays class to sort the array I had to extend Comparable and make compareTo() concrete.

I am good up to here but the bonus question is getting me. It is with just one List Iterator (no other variables) print a message if there are any duplicate integers in the List Iterator.

I have been looking at the API and trying things like If ((record.next)).equals(record.previous) but haven't got it to work without fail.

Any help would be appreciated.

This post was edited by ROM on Oct 25 2015 08:54am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 25 2015 09:09am
is this what you're using?
http://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html

your instructions said it's sorted at some point?

here's where i'd start. loop while it has next. if it has a previous, then compare next with previous. if they're the same, there are dupes. regardless of whether it had a previous, call next() to advance the cursor since we didn't actually move forward yet (either didn't move at all or moved forward one then back one)

edge cases are the front of the list and the end of the list.

This post was edited by carteblanche on Oct 25 2015 09:09am
Member
Posts: 29,350
Joined: Mar 27 2008
Gold: 504.69
Oct 25 2015 09:12am
Quote (carteblanche @ Oct 25 2015 11:09am)
is this what you're using?
http://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html

your instructions said it's sorted at some point?

here's where i'd start. loop while it has next. if it has a previous, then compare next with previous. if they're the same, there are dupes. regardless of whether it had a previous, call next() to advance the cursor since we didn't actually move forward yet (either didn't move at all or moved forward one then back one)


Yes that was the API I was looking at and yes it was sorted prior to trying to find the duplicates. I think you hit on something important for me, I was never advancing the cursor.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 25 2015 09:17am
Quote (ROM @ Oct 25 2015 11:12am)
Yes that was the API I was looking at and yes it was sorted prior to trying to find the duplicates. I think you hit on something important for me, I was never advancing the cursor.


learn to debug or log and you'll discover the issue very quickly next time. personally, i'm a logger, but there's nothing wrong with debugging.
Member
Posts: 29,350
Joined: Mar 27 2008
Gold: 504.69
Oct 25 2015 09:19am
Quote (carteblanche @ Oct 25 2015 11:17am)
learn to debug or log and you'll discover the issue very quickly next time. personally, i'm a logger, but there's nothing wrong with debugging.


Yeah. It is kind of weird but my school doesn't really put emphases on debugging. I will report back after I try to code something up.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 25 2015 09:31am
Quote (ROM @ Oct 25 2015 11:19am)
Yeah. It is kind of weird but my school doesn't really put emphases on debugging. I will report back after I try to code something up.


I'm not fond of how schools handle their curriculum. Or maybe i'm just biased by my own shitty school. Don't let schools get in the way of your learning.
Member
Posts: 29,350
Joined: Mar 27 2008
Gold: 504.69
Oct 25 2015 09:36am
Code
System.out.printf("\nDetect Duplicates:\n");

ListIterator recordIterator = recordLinkedList.listIterator();


while(recordIterator.hasNext())
{

if(recordIterator.hasPrevious())
{
if(recordIterator.next()==recordIterator.previous())
{
System.out.println("There is a duplicate");
}
else if(recordIterator.next()!=recordIterator.previous())
{
System.out.println("There are no duplicates");
}
}

recordIterator.next();
}


Stuck :lol:
None of the records are the same integer and all I get is There are duplicates four times.
I've tried
Code
if (recordIterator.next().equals(recordIterator.previous()))
and get the same result.

This post was edited by ROM on Oct 25 2015 09:37am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 25 2015 09:51am
Quote (ROM @ Oct 25 2015 11:36am)
Code
System.out.printf("\nDetect Duplicates:\n");

ListIterator recordIterator = recordLinkedList.listIterator();


while(recordIterator.hasNext())
{

if(recordIterator.hasPrevious())
{
if(recordIterator.next()==recordIterator.previous())
{
System.out.println("There is a duplicate");
}
else if(recordIterator.next()!=recordIterator.previous())
{
System.out.println("There are no duplicates");
}
}

recordIterator.next();
}


Stuck :lol:
None of the records are the same integer and all I get is There are duplicates four times.
I've tried
Code
if (recordIterator.next().equals(recordIterator.previous()))
and get the same result.


I don't see any logging bro.

I suggest creating a wrapper around the iterator. then every time you call next() or previous() it will log what value is getting returned and the cursor position.

eg:
Code
ListIterator recordIterator = new ListIteratorWithLogging(recordLinkedList.listIterator());


where:
Code
class ListIteratorWithLogging implements ListIterator{
ListIterator _source = null;
Object _current = null;
public ListIteratorWithLogging(ListIterator source){
_source = source;
}
public Object next(){
Object nextValue = _source.next();
log.debug("next() moved from " + _current + " to " + nextValue);
_current = nextValue;
return nextValue;
}
// other methods
}


that way when you're ready to turn in your assignment, you simply change one line of code where you use your wrapper and dont have to worry about removing logging or extraneous variables.

This post was edited by carteblanche on Oct 25 2015 09:54am
Member
Posts: 29,350
Joined: Mar 27 2008
Gold: 504.69
Oct 25 2015 10:43am
Quote
Detect Duplicates:
next() moved from null to 4
There is a duplicate
next() moved from 4 to 8
next() moved from 8 to 12
There is a duplicate
next() moved from 12 to 16
next() moved from 16 to 20
There is a duplicate
Exception in thread "main" java.util.NoSuchElementException


:lol: Cool. Now to figure out what is wrong.

This post was edited by ROM on Oct 25 2015 10:53am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 25 2015 11:00am
Quote (ROM @ Oct 25 2015 12:43pm)
:lol: Cool. Now to figure out what is wrong.


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
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll