?
Solved

Java iterator problem

Posted on 2013-12-29
2
Medium Priority
?
416 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

770 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