Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code seems to give the right combination of Numbers, but it fails to give the right combination of Index.</p> <p>This is because you are calling <code>pop</code> function which removes an item from a Stack thereby decreasing its size by 1. Hence the Index you get, is the index as compared to the Stack's size at that instant.</p> <p>Instead I would suggest, to use <code>peek()</code> or <code>get(int index)</code> function to read the values. I have updated your example with <code>get(index)</code> without cloning the Stack...hv a look...</p> <pre><code>import java.util.Stack; public class Class2 { public static void main(String[] args) { int x = 100; Stack stack=new Stack(); Stack tempStack=new Stack(); stack.push(new Integer(20)); stack.push(new Integer(53)); stack.push(new Integer(41)); stack.push(new Integer(38)); stack.push(new Integer(28)); stack.push(new Integer(47)); stack.push(new Integer(70)); stack.push(new Integer(30)); stack.push(new Integer(80)); stack.push(new Integer(400)); stack.push(new Integer(3)); stack.push(new Integer(20)); // tempStack = (Stack) stack.clone(); for (int i=0; i&lt;stack.size(); i++) { // tempStack = (Stack) stack.clone(); int value = (Integer) stack.get(i); if (!stack.isEmpty()) { for (int k=i+1; k&lt;stack.size()-1; k++) { int tmp = (Integer) stack.get(k); System.out.println("Value"+value+" tmp "+tmp+"Stack size"+stack.size()); if ((value + tmp) == x) { System.out.println("Indices " + i + " &amp; " + k + " with values " + value + " &amp; " + tmp); } } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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