Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Stacks - checking if 2 numbers of the stack equal 100

Tags:

java

arrays

stack

I have to check the sum of which 2 values in the stack equal 100 and print out the indecis and the numbers. I have made this possible using arrays but i can't get it to work using stacks. Please help me. I have wrote the following until now and it doesn't give me the right output.

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 my 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);
        }
    }
}
}
like image 553
user1234905 Avatar asked Jan 17 '23 09:01

user1234905


1 Answers

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

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

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

However, the compiler won't convert between int[] and Integer[] so you'd have to do:

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

And using Integer[] would be a chore.

So the easiest thing to do is this:

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 repeatedly cloning and emptying a Stack.

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

like image 110
Dave Webb Avatar answered Jan 31 '23 20:01

Dave Webb