• 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

Doubt in assertion

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 144
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

.I have a doubt in the following code...



I was going by above line
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic