2 Replies Latest reply: May 18, 2007 1:33 AM by 807600 RSS

    arrays implement stack...I don't get it

    807600
      I'm having trouble trying to set up a stack to implement an arrayI. I
      realize there is an array declared "a" that has 10 elements. So I want to assign the first element to the array. I'm not understanding how to begin the array. I've tried different variables, each are causing errors. Here's what I've got, I'm stuck in case 1 trying to assign and set the first value to the array???

      // StackArraySimple.java
      // Implements a stack with an array, pushes and pops, and prints out the stack
      // Dansing


      import java.util.*;
      public class StackArraySimple {
      public static void main(String[] args) {

      Scanner in=new Scanner(System.in);
      String[] a = new String[10];
      int index = 0;


      System.out.println ("Program Strings-in-a-Stack Using an Array");
      System.out.println ("==============================================");
      int choice = 1; //dummy value so while loop will run at least once

      while (choice != 4) {
      //while user has not chosen to quit do the following

      System.out.print("Select: 1) add item; 2) remove item; 3) print; 4)
      quit:"); //print options
      choice = in.nextInt(); //get the option chosen by user

      switch (choice) { //do one of three courses of action, given selection by user

      case 1: //get the data to push onto the stack and push it on

      System.out.print("Please enter your string: "); // solicit a new string
      String value = in.next(); //get the new string

      public void push(String value) { // this is causing me problems...how to get it to assign the first value in the array
      a[++index] = j;
      }
      // assign that value to the next place in the array
      //after value has been assigned to current top of stack, increment pointer
      System.out.println("After push value of index: " + index);
      //check on value
      break;
      *************************

      I'm trying to "push" the first "value" on using a method called push.
      help....Dansing
        • 1. Re: arrays implement stack...I don't get it
          807600
          1. You have the push method inside the main method. Not allowed to have one method inside another.

          2. This:
          a[++index] = j;
          will increment index from 0 to 1 and then make the assigment, thus leaving element 0 of the array empty.
          • 2. Re: arrays implement stack...I don't get it
            807600
            Hi,
            Here's another way to implement the push method:
            public void push(String val){
            
                if(stack[stack.length - 1] != null){
                     System.out.println("Stack is full");
                     return;
                }
                String[] temp = new String[stack.length];
                if(stack[0] != null){
                     System.arraycopy(stack,0,temp,0,stack.length);
                     stack[0] = val;
                     System.arraycopy(temp,0,stack,1,temp.length - 1);
                }
                else if(stack[0] == null){
                     stack[0] = val;
                }
            }
            Regards.