This week's book giveaway is in the Spring forum.
We're giving away four copies of Modern frontends with htmx and have Wim Deblauwe on-line!
See this thread for details.
Win a copy of Modern frontends with htmx this week in the Spring 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 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

for loop and arrays

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clarify further:
 
reply
    Bookmark Topic Watch Topic
  • New Topic