This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in assertion

 
sangeetha raj
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Am new to this forum..I am preparing for scjp 6..I have a doubt in the following code..

I found that there is an error in line 6,because we can't input an array into an int..But they have given its correct in the scjp 6 (kathy sierra) book , self test section question number 10..Please clear my doubt..correct me if am wrong..Thanks in advance..

(In the book)They have given this as answer : 1 1 3 3 9 9

public class Circles {
4. public static void main(String[] args) {
5. int[] ia = {1,3,5,7,9};
6. for(int x : ia) {
7. for(int j = 0; j < 3; j++) {
8. if(x > 4 && x < 8) continue;
9. System.out.print(" " + x);
10. if(j == 1) break;
11. continue;
12. }
13. continue;
14. }
15. }
16. }

Thanks,SR
 
Zeeshan Sheikh
Ranch Hand
Posts: 144
Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure why you think there is an error on line 6. Did you try to compile it yourself. All its doing passing reference of ia to int x which in turn you will get value at specific index. Try this

do after first loop and see the values.

Hope this helps.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sangeetha raj,

Welcome to CodeRanch!

I see that title of your post is 'Doubt in assertion', however, I do not see a single assertion in code provided by you?

Are you sure you have doubt in assertion? and that you have posted correct code?
 
Zeeshan Sheikh
Ranch Hand
Posts: 144
Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.I have a doubt in the following code...


I was going by above line
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sangeetha. Welcome to The Ranch!

Are you saying that you think that for(int x : ia) { ... } is assigning the array ia to x? That would cause an error, but that's not what's happening. This is the enhanced for loop that was introduced in Java 5, and it's assigning each element of ia in turn to x.
 
Helen Ma
Ranch Hand
Posts: 451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the purpose of the exam, enhanced loop is for array or any object that extends the Iterable interface such as list.
Here are the valid statement for enhanced for loop:
for (int x : ia) where ia is int [] ;
for (int[] x: ia) where ia is int[][];
for (Object x: list) where list is linked list or arraylist.
for (Object x : myHashSet) where myHashSet is a HashSet.

However, in the exam, you may have this:
for (Object x: myHashMap) where myHashMap is a HashMap type.
It seems to work. But it won't compile because HashMap does not implement Iterable interface.
For more detail, please refer to Mughal and Rasmussen's study guide.
Correct me if I am wrong.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic