aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes queries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "queries" Watch "queries" New topic
Author

queries

Shaily Sharma
Ranch Hand

Joined: Sep 10, 2007
Posts: 39
1)
int []arr = {1,2,3,4};
for ( int i : arr )
{
arr[i] = 0;
}
for ( int i : arr )
{
System.out.println(i);
}


it prints 0 0 3 0.
How come 3 is coming into the picture??

2)
public static void main(String [] args)
{
System.out.println(m(2));
}
static int m(int i)
{
int ret;
if( i == 2 )
{
ret = i;
}
return ret;
}

there is a compiler error: variable ret might not have been initialized
However if I modify this method like:

static int m(int i)
{
int y;
int ret;

if( i == 2 )
{
ret = i;
y =i;

}

return ret;
}
It doesnt give any error for y, we are using y w/o initializing.. Howcome?
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424


Here you get compiler error because the compiler thinks that variable ref might have not get any value, the if statement that assigns the value to it may never run, hence using it would give error, in your second example you didnt attempt to use variable y so no error.


It doesnt give any error for y, we are using y w/o initializing.. Howcome?

You are not using y, only assigned a value..
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Hi Shaily !!!

1) For the first point:

Let us have int arr[] = {1,2,3,4};

When we enter the loop:



i = 1 --> arr[1] = 0; Now arr[] = {1,0,3,4}
i = 0 --> arr[0] = 0; Now arr[] = {0,0,3,4}
i = 3 --> arr[3] = 0; Now arr[] = {0,0,3,0}
i = 0 --> arr[0] = 0; Now arr[] = {0,0,3,0}

At the end of the loop We have arr is {0,0,3,0}

2) For the second point:



If i != 2 ret is not initialized and the compiler will complain because you are trying to return ant integer that does not exist.


SCJP 5.0 | SCWCD 1.4
Shaily Sharma
Ranch Hand

Joined: Sep 10, 2007
Posts: 39
Thanks collins!!

Can anyone tell how advance for loop works internally, especially
exactly how index of an array is used. as per my understandings E.g
for(int i: n), here i refers to individual element in array n and not an index. If I want to use index for this array n, using this for loop method, how to go abt it?

If i want to use decrementing adv. for loop, is it possible? if yes how.
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Shaily,
This is enhanced for loop in java so
for (int i: arr) implies that i will have the value of arr and arr will go to next array value.
So as for the below example-:

int []arr = {1,2,3,4};
for ( int i : arr )
{
arr[i] = 0;
}
for ( int i : arr )
{
System.out.println(i);
}


Loop goes as-:
1st-: for(int i:arr)=>i=arr[0], i=1 and in for loop arr[i] = 0;=> arr[1]=0
2nd-: for(int i:arr)=>i=arr[1], i=0 and in for loop arr[i] = 0;=> arr[0]=0
3rd-: for(int i:arr)=>i=arr[2], i=3 and in for loop arr[i] = 0;=> arr[3]=0
4rth-:for(int i:arr)=>i=arr[3], i=0 and in for loop arr[i] = 0;=> arr[0]=0

so our final array is as-: arr={0,0,3,0}

So our result comes as 0,0,3,0

Hope this clears it
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Originally posted by Shaily Sharma:

... If I want to use index for this array n, using this for loop method, how to go abt it? ...

If i want to use decrementing adv. for loop, is it possible? if yes how.


Hi Shaily,

To access array elements by index, use the "standard" for loop as follows:


Regarding a decrementing enhanced for loop, afaik, there isn't one (at least that I've read about). You can easily do it this way though:

Hope this helps...
Aloha,
Doug
 
 
subject: queries