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

queries

 
Shaily Sharma
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shaily Sharma
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic