Java - Stacks - check if 2 stack numbers are 100

I need to check the sum of which 2 values ​​on the stack are 100, and print indecis and numbers. I made this possible using arrays, but I can't get it to work using stacks. Please help me. I have written the following so far, and this does not give me the correct result.

import java.util.Stack; public class 2 { 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<stack.size(); i++) { tempStack = (Stack) stack.clone(); int value = (Integer) stack.pop(); if (!stack.isEmpty()) { for (int k=0; k<tempStack.size(); k++) { int tmp = (Integer) tempStack.pop(); if ((value + tmp) == x) { System.out.println("Indices " + i + " & " + k + " with values " + value + " & " + tmp); } } } } } } 

The following is an array based solution:

 public class 1 { public static void main(String[] args) { int x = 100; int [] array = {20,3,400,80,30,70,20,47,28,38,41,53,20}; for (int i=0; i<array.length; i++){ int temp1 = array[i]; for (int k=1; k<array.length; k++) { int temp2 = array[k]; if ((temp1+temp2)==x) System.out.println("Indices " + i + " & " + k + " with values " + temp1 + " & " + temp2); } } } } 
+4
source share
6 answers

As Stack is a Collection it implements the toArray(T[]) method so that you can use this to convert your stack to an array and use the solution to your working array.

However, you will have a problem that there is no autoboxing for arrays. Autoboxing automatically converts between primitive types and objects, which means, for example, that you can add int values ​​directly to your Stack without creating Integer objects, since the compiler does this for you:

 Stack<Integer> stack = new Stack<Integer>(); stack.push(20); stack.push(53); 

However, the compiler will not convert between int[] and Integer[] , so you will need to:

 Integer[] array = stack.toArray(new Integer[stack.size()]); 

And using Integer[] would be a chorus.

So, the easiest way to do this is:

 int[] array = new int[stack.size()]; for (int i = 0; i < array.length; i++) { array[i] = stack.get(i); } 

Creating an array once will be more efficient than re-cloning and emptying the stack.

(Although if this is a homework question designed to teach you how to use stacks, this might not be the best approach!)

+4
source

A small change in the logic of the tour, do not accept stack.size() in the loop, it decreases at each iteration of the loop, therefore iteration of only half of the loop

 int stackSize = stack.size(); for (int i=0; i<stackSize; i++) { tempStack = (Stack) stack.clone(); int value = (Integer) stack.pop(); if (!stack.isEmpty()) { int tempSize = tempStack.size(); for (int k=0; k<tempSize; k++) { int tmp = (Integer) tempStack.pop(); if ((value + tmp) == x) { System.out.println("Indices " + i + " & " + k + " with values " + value + " & " + tmp); } } } } 
+1
source

The index of the second stack is probably incorrect, since when cloning the original stack inside the loop, it is less than each iteration.

i.e.

 stack = {25,50} stack.clone => {25,50} stack.pop => 25 stack.clone => {50} thus, if 50+50== 100 the indicies found would be i=1, k=0 instead of 1,1... 
+1
source

Your code seems to give the correct Numbers combination, but not the correct index combination.

This is because you call the pop function, which removes the element from the stack, thereby reducing its size by 1. Therefore, the index you get is the index compared to the size of the stack at this point.

Instead, I suggest using the peek() or get(int index) function to read the values. I updated your example with get(index) without cloning Stack ... hv a look ...

 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<stack.size(); i++) { // tempStack = (Stack) stack.clone(); int value = (Integer) stack.get(i); if (!stack.isEmpty()) { for (int k=i+1; k<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 + " & " + k + " with values " + value + " & " + tmp); } } } } } } 
+1
source

This is an unnatural use of the stack (want to know how to do this in the “stack oriented” Forth? Would you use an array.), So it’s correct that you are fighting. However, just use an array solution after implementing these stack statements:

  • DEPTH, which returns the number of items on the stack.

  • PICK, which returns an item by index onto the stack.

If you are allowed to use them, java.util.Stack inherits both: .size() and .elementAt()

+1
source

The problem with your basic stack solution is on this line. int value = (Integer) stack.pop (); as soon as you put the first element, it will disappear from the stack.

+1
source

Source: https://habr.com/ru/post/1398547/


All Articles