Note that there are some explanatory texts on larger screens.

plurals
  1. POJAVA: Simple pop does only return first item
    text
    copied!<p>I got following code set up:</p> <pre><code>public class ListStack implements Stack { private class List { List next; Object object; public List(Object o, List n) { object = o; next = n; } } private List firstItem; private int size; public ListStack() { firstItem = new List(null, null); size = 0; } public List getEnd() { List endEl = firstItem; while (endEl.next != null) { endEl = endEl.next; } return endEl; } public boolean push(Object o) { List e1 = new List(o, null); this.getEnd().next = e1; size++; return true; } public Object pop() { if (this.firstItem.next == null) { return null; } else { List endEl; List tempEl; endEl = this.getEnd(); tempEl = firstItem; while (tempEl.next != endEl) { tempEl = tempEl.next; } tempEl.next = null; size--; return tempEl.object; } } public int size() { return size; } public static void main(String[] args) { Stack s = new ListStack(); Object test = new Object(); Object test2 = new Object(); System.out.println("pushing Object test to List: " + s.push(test)); System.out.println("pushing Object test2 to List: " + s.push(test2)); System.out.println("popping Object from List: " + s.pop()); System.out.println("popping Object from List: " + s.pop()); System.out.println("popping Object from List: " + s.pop()); } } </code></pre> <p>And this one:</p> <pre><code> public interface Stack { public int size(); public boolean push(Object o); public Object pop(); } </code></pre> <p>But its only giving me the first object and twice "null" but it should give me the two objects :( where is my mistake? It is asking for the last item and gives it back (.object) but only returns first object adress</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload