aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for loop and arrays 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 "for loop and arrays" Watch "for loop and arrays" New topic
Author

for loop and arrays

radhika ayirala
Greenhorn

Joined: Aug 28, 2007
Posts: 24
FIRST PROGRAM:
class test1
{
public static void main(String args[])
{
int []arr = {1,2,3,4};
for ( int i : arr ) -------------------------->1
{
arr[i] = 0;
System.out.println(i);
}

for ( int i : arr )
{
System.out.println(i);
}
}
}

SECONDPROGRAM:

class test2
{
public static void main(String args[])
{
int []arr = {1,2,3,4};
for ( int i =0;i<arr.length;i++ )------------------>2
{
arr[i] = 0;
System.out.println("one"+i);
}

for ( int i : arr )
{
System.out.println("two"+i);
}
}
}


My doubts are.
1. Are the 2 for loops : for ( int i =0;i<arr.length;i++ )
for ( int i : arr )
same?
2.If they are same,how come output is different.

3.Why the output of program1's 2nd for loop is not "0000"?
please explain.
radhika ayirala
Greenhorn

Joined: Aug 28, 2007
Posts: 24
I am sorry,let me reformat the question.

PROGRAM1:
class test1
{
public static void main(String args[])
{
int []arr = {1,2,3,4};
for ( int i : arr )
{
arr[i] = 0;
System.out.println(arr[i]);
}

for ( int i : arr )
{
System.out.println(i);
}
}
}

PROGRAM2:
class test2
{
public static void main(String args[])
{
int []arr = {1,2,3,4};
System.out.println("arr.length"+arr.length);
for ( int i =0;i<arr.length;i++ )
{
arr[i] = 0;
System.out.println("one"+arr[i]);
}

for ( int i : arr )
{
System.out.println("two"+i);
}
}
}

Why the ouput of program1 is not "0000".please explain.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14164
    
  19

1. Are the 2 for loops : for ( int i =0;i<arr.length;i++ )
for ( int i : arr )
same?

No.

With "for (int i = 0; i < arr.length; i++)", the variable i is used to index the array.

With "for (int i : arr)", i is used to hold the value of an element of the array.

So the meaning of the variable i is different for these two loops.
2.If they are same,how come output is different.

They are not the same.
3.Why the output of program1's 2nd for loop is not "0000"?

Because this is what happens in the first for-loop:

1. In the first iteration, i is assigned the first element in arr: i = arr[0] = 1
2. Then, the line "arr[i] = 0;" sets the second element (arr[1]) to 0; the array now contains { 1, 0, 3, 4 }
3. Second iteration: i is assigned the second element in arr: i = arr[1] = 0
4. The line "arr[i] = 0;" sets arr[0] to 0; the array now contains { 0, 0, 3, 4 }
5. Third iteration: i is assigned the third element in arr: i = arr[2] = 3
6. The line "arr[i] = 0;" sets arr[3] to 0; the array now contains { 0, 0, 3, 0 }
7. Fourth iteration: i is assigned the fourth element in arr: i = arr[3] = 0
8. The line "arr[i] = 0;" sets arr[0] to 0; the array now contains { 0, 0, 3, 0 } (no change)

So at the end the array contains { 0, 0, 3, 0 }, and not { 0, 0, 0, 0 }

Note: You can use code tags when you post code, so that the forum can display your code properly.
[ September 19, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14164
    
  19

To clarify further:
 
 
subject: for loop and arrays