Solved

Java iterator problem

Posted on 2013-12-29
2
414 Views
Last Modified: 2013-12-30
The following code is indicating an error at the line c = itr.next(). I can eradicate the error by casting itr.next() as follows:

c = (Customer) itr.next();

Is this a valid way of addressing the issue ? I am a seasoned C/C++ programmer but am totally new to Java. Customer is a class


The code is as follows:
// declared at the top of the class:-
   ArrayList<Customer> customerList  = new ArrayList<>();

// code in a member function:-
   public Customer find(int custID)
    {// iterate the array, add matching results to a second array and return the second
       Iterator itr = customerList.iterator();
       Customer c = null;

 
       while(itr.hasNext())
       {
           // the line below gives an error; incompatible types, required customer, found Object
 
           c = itr.next();
           if(c.matchesID(custID))
               break;
       
       }
       return c;
    }
0
Comment
Question by:alcindor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 39745155
The Iterator interface is generic, like the ArrayList class. If you declare your variable like this, the build error should go away:

Iterator<Customer> itr = customerList.iterator();

Open in new window


The way you're doing it, with the explicit cast, is how Java programmers had to do it before the language added support for generics. It's what the language is doing in the background either way, so it's not necessarily bad, just no longer necessary.
0
 
LVL 2

Author Closing Comment

by:alcindor
ID: 39746460
Excellent. Thanks for that, Roger
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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