Results 1 to 12 of 12
  1. #1
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Creating Stack with Arrays

    Well my professor pretty much hinted that for our final one of the questions was gonna to be that we have to write the push method of a stack that is implemented with arrays.

    Java Code:
    public void push(Object o){
    	if(isFull()){  //this method would have to return true or false
    		resize(this.array); //resizes the array
    	}
    
    	current++;
    	array[current] = o; //adds the array
    	
    }

    i was wondering would be enough? i was gonna program the entire stack but i do not know how to make the array be changeable, i.e. integers, strings, etc.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Creating Stack with Arrays

    Have you tested it?

    Personally I would have the last two lines in reverse order. The "current" variable would have an initial value of zero. Insert object at index zero and then increment current. Even better:
    Java Code:
    array[current++] = o;

  3. #3
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: Creating Stack with Arrays

    nah i have not tested it, i do not have the entire code for it, he said it will just be that small portion,
    oh that makes sense
    im just curious if the logic of the program makes sense
    and is Object o correct?
    as in stack.push("hello"); or stack.push(5);
    would the parameter object o work?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Creating Stack with Arrays

    It depends upon the overall requirements (how the question is worded). stack.push(5) certainly won't work as 5 is a primitive and not an object.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Creating Stack with Arrays

    BTW it wouldn't take long to write a Stack class and test it.

  6. #6
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: Creating Stack with Arrays

    well im stuck, like list you can changes the type, like list<String> or list<Integer>
    anyway to make that work for arrays to make it changeable upon declaration?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Creating Stack with Arrays

    I'm not sure what you are asking but you can certainly write your own Generic Stack class.
    Java Code:
    class MySpecialStack<E> {
    
    }
    Then create an instance
    Java Code:
    MySpecialStack<String> mss = new MySpecialStack<String>();
    Now you can only "push" String objects onto the Stack.

  8. #8
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: Creating Stack with Arrays

    Okay that really helps me out, so i got something like this , but i getting a problem with the generic array
    Java Code:
    package ArrayStack;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Stack<E> {
    	private E[] array;
    	private int current = 0;
    
    	public Stack(int initialLength) {
    		if (initialLength > 0) {
    			array = (E[]) new E[initialLength];
    		} else{
    			array = (E[]) new E[10];
    		}
    	}
    
    	public void push(E e) {
    		//check for full array goes here
    		//resize
    		
    		array[current++] = e;
    		
    
    	}
    
    }

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Creating Stack with Arrays

    You cannot use Generics with arrays. You can only use it with the Collections Framework (Lists, Stacks etc).

  10. #10
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: Creating Stack with Arrays

    hmm, how would i go about fixing that?

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Creating Stack with Arrays

    What did you do in the first reply?

  12. #12
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: Creating Stack with Arrays

    Oh i believe i got it!
    this is just a rough draft im using to study it, havent implemented everything but it does what i want it to do for now
    Java Code:
    public class Stack<E> {
    	private E[] array;
    	private int current = 0;
    
    	public Stack(int initialLength) {
    		if (initialLength > 0) {
    			array = (E[]) new Object[initialLength];
    		} else{
    			array = (E[]) new Object[10];
    		}
    	}
    
    	public void push(E e) {
    		//check for full array goes here
    		//resize
    		
    		array[current++] = e;
    	}
    	public E peek(){
    		
    		return array[current -1];
    	}
    	public E pop(){
    		E test = array[current -1];
    		array[current -1] = null;
    		current--;
    		return test;
    	}
    	public static void main(String[] args){
    		Stack<Integer> stack = new Stack<Integer>(3);
    		stack.push(10);
    		stack.push(11);
    		stack.push(100);
    		stack.pop();
    		
    		System.out.println(stack.peek());
    	}
    
    }
    thanks for the help Junky i really appreciate it!

Similar Threads

  1. Help with creating a HashMap from Arrays
    By lannie1980 in forum New To Java
    Replies: 9
    Last Post: 04-24-2012, 10:39 PM
  2. Problem creating arrays
    By JohnPringle83 in forum New To Java
    Replies: 7
    Last Post: 05-06-2011, 11:01 PM
  3. trouble creating stack using array
    By shashankc in forum New To Java
    Replies: 5
    Last Post: 01-20-2011, 12:49 PM
  4. Creating generic arrays
    By stijn1989 in forum Advanced Java
    Replies: 6
    Last Post: 11-18-2009, 01:26 PM
  5. Creating a stack with data
    By ai_2007 in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 03:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •