Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Test printout of dequetester class giving strange results

ChisChis Member Posts: 1
My teacher assigned me to create a deque and dequetester class, which includes a printout of all the elements in the deque.

Here is what I have for the deque class:

import java.util.LinkedList;
import java.util.Iterator;

public class Deque implements Iterable
{
public Deque()
{
list = new LinkedList();
}
public void addFirst(String str)
{
list.addFirst(str);
}
public String removeFirst()
{
return (list.removeFirst()).toString();
}
public void addLast(String str)
{
list.addLast(str);
}
public String removeLast()
{
return (list.removeLast()).toString();
}

public Iterator iterator()
{
return list.iterator();
}

private LinkedList list;
}

-----
Here is what I have for the dequetester class:

public class DequeTester
{
public static void main (String[] args)
{
Deque dequetest = new Deque();
dequetest.addFirst("a");
dequetest.addLast("b");
dequetest.addLast("c");
dequetest.addLast("d");
dequetest.addLast("e");
dequetest.addLast("f");
dequetest.addLast("g");
for (Object e : dequetest)
{
System.out.println(dequetest.iterator() + "");
}
}
}

-----
(BlueJ keeps telling me that the dequetester class has unsafe methods. Is there a reason for this?)

The problem I'm having is that although the code runs through, the printout looks something like this:

java.util.LinkedList$ListItr@430b6
java.util.LinkedList$ListItr@11a01dd
java.util.LinkedList$ListItr@70610a
java.util.LinkedList$ListItr@1f31652
java.util.LinkedList$ListItr@3e96cf
java.util.LinkedList$ListItr@11ef443
java.util.LinkedList$ListItr@1367e28

The combinations at the end change everytime I run it. I tried using the toString method too, with no luck. I've heard that the toString method sometimes returns data that doesn't reflect the true nature of the objects. Does anyone know how to remedy this?
Thanks,
Chis :)

Comments

  • gopalgajaraogopalgajarao Member Posts: 2
    Use the snippet-
    [code] Iterator it = dequetest.iterator();
    while(it.hasNext())
    {
    System.out.println(it.next());
    }
    [/code]
    in the place of the following code-
    [code]
    for (Object e : dequetest)
    {
    System.out.println(dequetest.iterator() + "");
    }
    [/code]

Sign In or Register to comment.