This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

for loop and arrays  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify further:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!