我在启动这个程序时遇到了麻烦。我应该编写一个程序,通过询问用户10个数字来填充ArrayList
。需要ArrayList和Stack的帮助
列表完成后,我要导航它,如果某个数字是偶数,请将其从ArrayList
中删除,并将数字设置为整数的Stack
。到目前为止,我有这个,但我如何得到栈困惑开始,这样我就可以把连号进去:
import java.io.* ;
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
ArrayList<Integer> test = new ArrayList<Integer>();
Stack<Integer> myStack = new Stack<Integer>();
System.out.print ("Enter Number: \n");
for (int i = 0; i < 10; i++) { //Put Into ArrayList
test.add(input.nextInt());
}
System.out.print("Contents of Array: " + test);
System.out.print("\n");
for (int i= 0; i < 10 ; i++) {
int item = myIterator.getNext();
if (item % 2 == 0) {
myListIterator.remove(); //removes it from the ArrayList
myStack.push(item); //puts it into the stack
}
}
System.out.print("Contents of Array afer numbers removed: " + test);
System.out.print("\n");
}
}
您遇到了什么问题? – hvgotcodes
无关,但因为这是作业,我会添加此评论。养成编码接口的习惯是个好主意。因此,不是声明ArrayList test = new ArrayList(),而是使用List test = new ArrayList()。对接口进行编码为您提供更大的灵活性,当需要改变实现时。 – Marvo