Solved

Java iterator problem

Posted on 2013-12-29
2
389 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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…

758 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

17 Experts available now in Live!

Get 1:1 Help Now