Solved

ArrayList Iterator Problem

Posted on 2008-10-19
2
892 Views
Last Modified: 2008-11-17
I am trying to loop though an ArrayList and remove all even numbers.  But I am having trouble with Integer to Object conversions.  Or am I going about the whole thing wrong?   There will always be only integers in this ArrayList and I need to do this with Iterators.

Thanks



import java.util.*; 

import java.lang.*;

import java.io.*; 
 

class IteratorDemo { 

public static void main(String args[]) { 
 

ArrayList<Integer> myLIST = new ArrayList<Integer>();   //Create List

 

 //Add Items to LIst

myLIST.add(3); 

myLIST.add(8); 

myLIST.add(4); 

myLIST.add(1); 

myLIST.add(9); 

myLIST.add(7); 

myLIST.add(2); 

myLIST.add(5); 

myLIST.add(6); 
 
 
 

// use iterator to display contents of myLIST 

System.out.print("Standard Output: "); 
 

Iterator itrX = myLIST.iterator();   //define Iterator and attach to mylist
 

while(itrX.hasNext()) {         //loop through items while there are items to loop through.
 

Object element = itrX.next(); 

System.out.print(element + " ");   // print out element
 

} 
 

System.out.println();   //BlankLine
 
 

// Go back through the list and delete all even numbers

System.out.print("Only Odds: "); 

itrX = myLIST.iterator();

while(itrX.hasNext()) {
 

Object intEL = itrX.next();
 
 
 

if( intEL %2==0 ){

 itrX.remove();         

}

} 

 

}

}

Open in new window

0
Comment
Question by:PriceConsulting
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22752901
Either use the generic Iterator<E> like this:
Iterator<Integer> itrX = myLIST.iterator();
Integer element = itrX.next();  // retrieving iterator.next() as Integer

OR you will have to cast the result of .next() to an Integer like this:
Iterator itrX = myLIST.iterator();
Integer element = (Integer)itrX.next();

Since you are already dealing with the generic version fo ArrayList, I would highly suggest option 1.
0
 
LVL 2

Expert Comment

by:szigeti
ID: 22952969
Here ya go:

I added a loop to print out the filtered odd numbers at the end, and changed:

Iterator itrX = myLIST.iterator();
to:
Iterator<Integer> itrX = myLIST.iterator();

and

Object intEL = itrX.next();
to
Integer intEL = itrX.next();

import java.util.*; 

import java.lang.*;

import java.io.*; 

 

class IteratorDemo { 

public static void main(String args[]) { 

 

ArrayList<Integer> myLIST = new ArrayList<Integer>();   //Create List

 

 //Add Items to LIst

myLIST.add(3); 

myLIST.add(8); 

myLIST.add(4); 

myLIST.add(1); 

myLIST.add(9); 

myLIST.add(7); 

myLIST.add(2); 

myLIST.add(5); 

myLIST.add(6); 

 

 

 

// use iterator to display contents of myLIST 

System.out.print("Standard Output: "); 

 

Iterator<Integer> itrX = myLIST.iterator();   //define Iterator and attach to mylist

 

while(itrX.hasNext()) {         //loop through items while there are items to loop through.

 

Integer element = itrX.next(); 

System.out.print(element + " ");   // print out element

 

} 

 

System.out.println();   //BlankLine

 

 

// Go back through the list and delete all even numbers
 

itrX = myLIST.iterator();

while(itrX.hasNext()) {

 

Integer intEL = itrX.next();

 

 

 

if( intEL %2==0 ){

 itrX.remove();         

}

} 
 

// Added another loop to print out the remaining odd numbers:

itrX = myLIST.iterator();

System.out.print("Only Odds: "); 

while(itrX.hasNext()) {         //loop through items while there are items to loop through.

	 

	Integer element = itrX.next(); 

	System.out.print(element + " ");   // print out element

	 

	} 

 

}

}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 84
base64 decode encode 12 96
create a gui in perl 3 45
Strange loading of website behaviour 3 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now