Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enhanced for loop

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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?
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • 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?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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)).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic