2017-02-21 109 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(); 
    } 

}

回答