aspose file tools*
The moose likes Beginning Java and the fly likes Array increment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array increment" Watch "Array increment" New topic
Author

Array increment

Wrick Sharma
Greenhorn

Joined: Apr 03, 2005
Posts: 17
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
You are missing your braces. That section of code should be:
[ November 16, 2005: Message edited by: C Clites ]
Wrick Sharma
Greenhorn

Joined: Apr 03, 2005
Posts: 17
Oops ... My bad ... thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array increment
 
Similar Threads
Merge array's in java?
arrays
Comparing Two ArrayLists
Identical Arrays
creating a deadlock.