# Java 8 | Two Dimensional Array Practice — Snake, Snail

Hi guys! Following the last post, today we’re going to practice the Two Dimensional Array! There’s a link of the last session below.

It’s always better when you solve the questions by yourself!

# [Question] Use the code below, put the numbers like the sample and print it out on the console.

`int[][] two = new int;`

## Sample 1. Snake

`25  24  23  22  2116  17  18  19  2015  14  13  12  116   7   8   9   105   4   3   2   1 `

So the numbers go like this,

The answer is on below.

As you see if the row’s index was a even number or 0, the numbers get bigger to the left, and if it’s a odd number, the numbers get bigger to the right. So first I made the for loop goes from the last row to the first row, and if the remainder of the row divided by 2 is 0, the column’s index gets smaller. If it’s not, the index gets bigger.

## Sample 2. Snail

`1   2   3   4   516  17  18  19  615  24  25  20  714  23  22  21  813  12  11  10  9`

The answer is on below.

As you see on the picture above, I put the different colors on every two arrows. I’m going to consider each two arrows as a group.

So you see on the first purple arrow, there are 5 numbers, and on the second there are 4. And on the first green arrow there are 4 numbers and on the second there are 3…. So all the second arrow gets one smaller!

So I declared the variable `max` as 5, which is the number that the first purple arrow has. And I made the first for loop’s variable `j` start from 0 and gets bigger as long as it’s smaller than `max` . So the first loop can be repeated for 5 times.

And Since the row’s index is all 0 on the first loop, I just put variable `row` which is declared as 0 in the brackets in front, and in the brackets behind I put the variable `col` , but before that, I made `col` into `col+move` so it can start from 0 and gets one bigger every time the for loop repeated. So at the end of the first loop the `col` will be 4.

And after the first for loop finished, I made `max` gets one smaller. So there are only 4 numbers can be stored on the second purple arrow. And this time, I made `row` into `row+move` so the row can start from 1 (cause last time it was 0) and gets one bigger every time the for loop repeated.

But uh-oh… Now on the first green arrow, the row has to get one smaller, and on the second green arrow the column has to get one smaller every time! What should we do here?

I multiplied -1 on the variable `move` so the move becomes `-1` ! And made first two for loops into nested loops so they can be repeated until the array’s full! When yellow arrows started the `move` will be multiplied by -1 again so it will get back to 1! And I put the variable `i` gets bigger as long as it’s smaller than the 5 which is the number of the group of the arrows!

Wow that was a lot to take in! I hope you all enjoyed our little practice session today, thank you for reading my post! See you next time!

--

--