wood burning stoves 2.0*
The moose likes Java in General and the fly likes Enhanced for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Enhanced for loop" Watch "Enhanced for loop" New topic
Author

Enhanced for loop

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

What is the error you are getting? Please tell the details.
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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

Joined: Oct 27, 2005
Posts: 19653
    
  18

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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)).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enhanced for loop
 
Similar Threads
Generics
Type safty warning in Generics
doubts in converting arraylists to array
Doubts regarding ArrayLists
Urgent Help!