I am having trouble starting out this program, I am suppose to write a program that will create an ArrayList, asking the user for 10 numbers. Then this will be put into the Array. Then after the list is made navigate it and if a number is even remove it from the ArrayList and copy it to a stack of integers.
import java.io.* ;
import java.util.*;
public class Test {
public static void main(String[] args){
Scanner input = new Scanner (System.in);
ArrayList<Integer> integers = new ArrayList<Integer>();
System.out.print ("Enter Number: \n");
for (int i = 0; i < 10; i++){
integers.add(input.nextInt());
}
for (int i= 0; i < 10 ; i++){
int item = myListIterator.getNext();
if(item % 2 == 0)
{
myListIterator.remove(); //removes it from the ArrayList
myStack.push(item); //puts it into the stack
}
}
Stack<Integer&开发者_如何学运维gt; st1;
st1 = new Stack <Integer> ();
}
}
Have a look at the official Lesson: The "Hello World!" Application to get started with a basic program.
Then I suggest you use a Scanner to read user input. You could initialize it like this
Scanner scanner = new Scanner(System.in);
and then read numbers from the user, using the Scanner.nextInt method. (Have a look at the official tutorial on The for Statement and you'll probably figure out how to do this ten times.)
Create the ArrayList like this
ArrayList<Integer> integers = new ArrayList<Integer>();
and put the provided numbers in the ArrayList using the add method.
Then I'd suggest you use an Iterator<Integer> (which you can get hold of from integers.iterator(), and loop while iterator.hasNext() and if iterator.next() is an even number, you do iterator.remove().
int[] numbers = {System.in};
for (int item : numbers) {
System.out.println("Count is: " + item);
doesn't look too good.
Try something like
for (int i = 0; i < 10; i++)
integers.add(input.nextInt());
Well so far you have the input from the user. Now you need to do the following:
- Check if the
currentvalue iseven, this is amodulusoperation - In the event that
currentiseven,push currentonto aStackandpopit from yourArrayList - Change the name of your homework assignment to something that is not
ArrayListunless that is what your professor asked - To validate the
Stackonly hasevenvalues, you may decide topopeverything off and print that information
You already have most of the code in place. You just need to add them to a stack. The java Stack class can be found here:
http://download.oracle.com/javase/6/docs/api/java/util/Stack.html
So constructing the ArrayList you just have to find even integers, remove them, and add it to the stack. Note that
if (i %2==0)
is incorrect, because we're not concerned about "i" being even, we're concerned about integers.get(i) being even. To loop through your ArrayList, you should use an Iterator. Using a manual index to find the even integers, while possible, is very prone to error (because you are constantly changing the size of the ArrayList!) Help can be found here:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Iterator.html
So you would have
int item = myListIterator.getNext();
if(item % 2 == 0)
{
myListIterator.remove(); //removes it from the ArrayList
myStack.push(item); //puts it into the stack
}
in your loop. The initialization of the Stack is pretty trivial and if this is a homework assignment, you should know how to do that so I will leave you to figure it out.
加载中,请稍侯......
精彩评论