File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


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

Array Question

Chris Nicholson
Greenhorn

Joined: May 10, 2011
Posts: 1
So I'm trying to figure out what exactly this array is saying at each step. I understand the loop but I don't really understand the results.


{
public static void main (String [] args) {
int [] a = new int[5];
----> for (int i=0; i < a.length; i++){
----> a[i] = a.length-i;
}

----> for (int i=0; i < a.length-1; i++){
----> a[i] = a[i+1];
}

// Print the elements in a
System.out.println(Arrays.toString(a));

int [] b = new int[5];
----> for (int i=1; i < a.length; i++){
----> b[i] = b[i-1];
}

// Print the elements in b
System.out.println(Arrays.toString(b));

}

run:
[4, 3, 2, 1, 1]
[0, 0, 0, 0, 0]
[2.0, 1.5, 1.0, 0.5, 0.5]
BUILD SUCCESSFUL (total time: 0 seconds)

Thanks guys!

Chris
sourabh girdhar
Ranch Hand

Joined: Feb 10, 2010
Posts: 71

Its not doing anything great or mysterious.

The output of this program comes as

[4, 3, 2, 1, 1]
[0, 0, 0, 0, 0]

For first loop it assigns the value as length -i .. and then i +1..

You can simply take a pen paper and get the same desired result in no time.
or if you want to calculate values at each step you can simply debug the program.

Thanks
Sourabh


SCJP SCWCD AIX SOA
The significant problems we face cannot be solved by the same level of thinking that created them -- Albert Einstein

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Welcome to the Ranch!

Let's try to translate this from Java to English...

Before the first for-loop, you're creating an array a that can contain 5 ints. Then in the first for-loop, you're letting i start from zero, and it goes up to (but not including) the length of a (which is 5), so i goes from 0 to 4 (inclusive).

Inside the loop, you're assigning a value to the element with index i of the array a. The value you're assigning is a.length - i. We know that a.length is 5, so this is: 5 - i. Writing out the five iterations explicitly, this leads to:

a[0] = a.length - 0 = 5
a[1] = a.length - 1 = 4
a[2] = a.length - 2 = 3
a[3] = a.length - 3 = 2
a[4] = a.length - 4 = 1

Then, in the second for-loop you let i go from 0 to the length of a minus one (exclusive), so a goes from 0 to 3 (inclusive). Writing out the iterations again, you get:

a[0] = a[0 + 1] = a[1] = 4 (because a[1] = 4 - see above)
a[1] = a[1 + 1] = a[2] = 3
a[2] = a[2 + 1] = a[3] = 2
a[3] = a[3 + 1] = a[4] = 1

Note that a[4] remains unchanged, so a[4] is still 1.

The content of the array is now [4, 3, 2, 1, 1] so that explains the first output.

It's similar for the third for-loop, with array b. Note that when you create a new int array, all its elements are initialized to 0. In the third for-loop you're assigning b[i - 1] to b[i], but since you've never set the elements of b to anything else than the default 0, the second output will be all zeroes.

You didn't post the code that produces the third line of output.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Question