• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enhanced for loop

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the error you are getting? Please tell the details.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the error I get :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
ar1 cannot be resolved


Is is required that I need to use Generics here ??? I read an article that said :

"The enhanced for loop makes the explicit use of an iterator unnecessary. Rather than create an Iterator object for the ArrayList and then use the iterator in the for loop, you use the following:

for ( Integer square : squares)

This indicates that the name of the collection is squares. It also indicates that the currently referenced item is of type Integer and is referenced by the variable square.

This code will not compile because there is no way of knowing that the contents of the ArrayList is of type Integer. To fix this, you need to use another feature introduced in J2SE 5.0, namely generics. You need to specify in the declaration and definition of squares that it can only hold elements of type Integer."

Please assist in understanding enhanced for loop when can we use it and when SHOULD WE NOT USE it.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jose chiramal wrote:This is the error I get :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
ar1 cannot be resolved


That's the runtime error. What is the compiler error? Never run code while you still have compiler errors; solve those first.

Is is required that I need to use Generics here ???


It is if you want to get elements from the ArrayList without casting yourself. However, your code still is broken; the contents of the ArrayList aren't ArrayLists but Integers.

Please assist in understanding enhanced for loop when can we use it and when SHOULD WE NOT USE it.


Your problem is not with the loop itself; after all, your for-each loop works if you get the element type correct. Your problem is generics. You can only retrieve an element from any Collection without casting if the Collection is a generic one. Yours isn't. Try changing your first line to "ArrayList<Integer> arl = new ArrayList<Integer>();". That tells the compiler that all elements of the ArrayList will either be an Integer or null. Of course that still won't fix your second loop but that's because now the compiler sees that the elements can never be ArrayList instances. The following will then work:
As for your question, you should use the for-each loop always* except when you need to remove elements while iterator, or need the array index. For instance:

* Well, always is maybe a bit harsh but it makes it easier to (read and) write, so why not?
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to "read out" the for-loop to understand what it does. Currently you do something like: "for each ArrayList al in arl do...", does that make sense?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 8 of your first post, you are calling it arl (three letters, 'A', 'R', 'L').

In line 14, you are calling it ar1 (two letters: 'A', 'R' and a digit: '1' (one)).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic