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

Array Question

 
Chris Nicholson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic