2017-02-21 110 views
0

我的任務是編寫一個測試程序,提示用戶輸入5個字符串,並使用MyStack和ArrayList以相反的順序顯示它們。我需要幫助瞭解如何接受用戶輸入並將其放入堆棧並反向打印。堆棧和數組列表

MyStack Class

MyMain

My Main: 

package arraylist; 

import java.util.Scanner; 

/** 
* 
* @author dghelardini 
*/ 
public class ArrayList { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     Scanner userIn = new Scanner(System.in); 
     System.out.print("Enter five names: "); 
    } 
} 


MyStack Class: 

package arraylist; 

/** 
* 
* @author dghelardini 
*/ 
public class MyStack extends ArrayList 
{ 
    private ArrayList<Object> theList = new ArrayList<>(); 

    public boolean isEmpty() 
    { 
     return theList.isEmpty(); 
    } 

    public int getSize() 
    { 
     return theList.size(); 
    } 

    public Object peek() 
    { 
     return theList.get(getSize()-1); 
    } 

    public Object pop() 
    { 
     Object o = theList.get(getSize()-1); 
     theList.remove(getSize()-1); 
     return o; 
    } 

    public void push(Object o) 
    { 
     theList.add(o); 
    } 

    @Override 
    public String toString() 
    { 

     return "stack:" + theList.toString(); 
    } 

}

回答