我的作业是编写一个测试程序,该程序提示用户的5个字符串,并使用Mystack和ArrayList以相反的顺序显示它们。我需要帮助弄清楚如何获取用户输入并将其放入堆栈中并反向打印。
神秘阶级类
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();
}
}
扫描仪:http://docs.oracle.com/javase/7/docs/api/java/java/util/scanner.html?is-external=true
用户进入字符串,然后调用堆栈以存储5个字符串。然后,当您弹出时,堆栈将返回最后一个项目