Hello there...I need some help when I'm trying to pop data (numbers) from an array into another one (push them). I managed perfectly to input data but I'm completly lost when trying to pop from one array and push it into another one, I tried many things but none of them worked...I'm not sure If I'm clear whith my question :S any help/advices would be helpful.
Here is my the code:

public class PushAndPopStacks implements Stack {
  protected int capacity;
  public static final int CAPACITY = 2;
  protected Object S[];
  protected Object Inv[];
  protected int top = -1;
  public PushAndPopStacks() {
  public PushAndPopStacks(int cap) {
    capacity = cap;
    S = new Object[capacity]; 
  public int size() { 
    return (top + 1);
  public boolean isEmpty() {
    return (top == -1);
  public void push(Object element) throws FullStackException {
    if (size() == capacity) {
    S[++top] = element;
  public Object top() throws EmptyStackException {
    if (isEmpty())
      throw new EmptyStackException("Stack is empty.");
    return S[top];
  public Object pop() throws EmptyStackException {
    Object element;
    if (isEmpty())
      throw new EmptyStackException("Stack is empty.");
    element = S[top];
    S[top--] = null;
    return element;

    private void doubleArray( ) {
      Object [ ] newArray;

      System.out.println("Stack is full (max size was "+capacity+"). Increasing to "+(2*capacity));
      //double variable capacity
      capacity = 2*capacity;
      newArray = new Object[ capacity ];
      for( int i = 0; i < S.length; i++ )
          newArray[ i ] = S[ i ];
      S = newArray;
    public void inverse(){
    public static void main(String[] args) {
        Stack S = new PushAndPopStacks();
      while (!S.isEmpty()) {

Edited by mike_2000_17: Fixed formatting

7 Years
Discussion Span
Last Post by javaAddict

What errors do you get? What is the output and why you thing it is wrong. Also I don't see where is the method for putting the data from one array to the other.
Maybe you should loop one array and put its values to another, but the 0 index should be put in the length-1, the i index in the length-1-i index


I tried many things and also have many errors so I just tried to paste here a clean code...
I just want to pop Data from the Array S and push it into Array Inv...


Then you need 2 stacks not one. You use a private array in the Stack class. You can create as many instance of the PushAndPopStacks class.
Create 2 PushAndPopStacks instances. You already have one:
Stack S = new PushAndPopStacks()

The PushAndPopStacks should have only one private array. Create 2 PushAndPopStacks. Then call pop method of one instance and push the result into the other.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.