Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array increment

 
Wrick Sharma
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas why this code is not compiling

public class Arrays {

public static void main(String[] args) {
int[] a1 = { 1, 2, 3, 4, 5 };
int[] a2;

a2 = a1;

for(int i = 0; i < a2.length; i++)
System.out.println (a2.length);
a2[i]++;

for(int i = 0; i < a1.length; i++)
System.out.println(
"a1[" + i + "] = " + a1[i]);
}
}

If i remove "System.out.println (a2.length); it compiles.
Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've neglected to put braces around the body of this "for" loop:

for(int i = 0; i < a2.length; i++)
System.out.println (a2.length);
a2[i]++;

So that the body is actually just the "println" statement, and at the third line, "i" is undefined. If you comment out the "println", then the assignment alone becomes the body of the loop.
 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing your braces. That section of code should be:
[ November 16, 2005: Message edited by: C Clites ]
 
Wrick Sharma
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops ... My bad ... thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic