Need help with ArrayList and Stack please
I am having trouble starting out this program. I am supposed to write a program that will populate an ArrayList
by asking the user for 10 numbers.
After the list is made I'm to navigate it and if a number is even number, remove it from the ArrayList
and put the number to a Stack
of integers. So far I have this but I am confused on how to get the stack started so that I can put the even numbers into it:
import java.io.* ;
import java.util.*;
public class Test {
publ开发者_JAVA百科ic 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");
}
}
It seems that you just need to initialize the stack. Do the initialization of the stack where you initialize the test array.
Put this:
Stack<Integer> item = new Stack <Integer> ();
After:
ArrayList<Integer> test = new ArrayList<Integer>();
EDIT -- I was feeling generous, so I finished it for ya ;) You were actually almost all the way there, so I don't feel I really deprived you of a learning opportunity. The only other real thing you were missing was initializing the iterator and using it correctly.
Note the following:
-- you will see that if you use the iterator, you can just get rid of the for loop.
-- I changed the names of the variables so they are a bit easier to follow-naming is important.
-- Finally, since an ArrayList ISA List, you will notice I changed the declaration for the input values to use the interface for the declaration.
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List<Integer> inputValues = new ArrayList<Integer>();
Stack<Integer> evens = new Stack <Integer> ();
System.out.print("Enter Number: \n");
for (int i = 0; i < 10; i++) { //Put Into ArrayList
inputValues.add(input.nextInt());
}
System.out.println("Contents of Array: " + inputValues);
Iterator<Integer> iter = inputValues.iterator();
while(iter.hasNext()) {
Integer currentVal = iter.next();
if (currentVal % 2 == 0) {
iter.remove(); //removes it from the ArrayList
evens.push(currentVal); //puts it into the stack
} else {
System.out.println("No");
}
}
System.out.println("List values " + inputValues);
System.out.println("Stack values " + evens);
}
}
Hint: The commented out code has a declaration and initialization of a stack that is suitable for your purposes. It needs to be before the code that pushes stuff onto the stack.
I really doubt your code compiles without modification. For example myListIterator and myStack aren't even declared and I don't remember java Iterators to have a getNext() method.
Before using a variable, you must first declare it and the initialize it, these operations can be both done in one line, for example : Stack<Integer> myStack = new Stack<Integer>();
Looking at the tags of your question, this seems to be some kind of homework, I'm sure the documentation explains all of theses steps.
And since your using a ListIterator to remove the Integer from the ArrayList, there's no need to use a for loop, you can do something like
while(myListIterator.hasNext()) {
Integer item = myListIterator.next();
if(item % 2 == 0) {
item.remove();
myStack.add(item);
}
}
精彩评论