Published in

Buzz Code

Java 8 | One Dimensional Array

One dimensional array — a collection of elements in a certain data type, in a horizontal style.

[One Dimensional Array]

Hi guys! Welcome back! Today I’ll be talking about the One Dimensional Array(AKA Single Dimension Array). One dimensional array is like a line of the rooms that can store the data in a certain type. It can be String, char, int…etc. If I want to make a array that can store the integers, the way to declare that will be like below.

Brackets `[]` stand for array. So it means that I declared the variable `a` as a array of the integer, and its length is 7. Here the length means there are 7 rooms to store data.

If we look inside of the array `a` it will be looking like this. The numbers in the braces are the index of the each room of the array. You can see the array’s index also starts with 0. And the numbers inside of the box are the value that each index has. I didn’t put any data in it yet, so its initial values are all 0. Array of the int has 0 as its initial value, and array of String has null.

To check the value of the array we can use the for statement here.

I put the variable `i` starts from 0, and made it increased as long as it’s smaller than the length of array. So the i will be the index of the array. And I printed `a[i]` , then it will be `a[0]`, `a[1]`, `a[2]``a[6]`. As you see, If I print them out, it shows the value of the each index has.

Now let’s bring the Random class back that we learned last time, and put the random numbers in the array. If you missed it, you can check the link below.

Array Practice) Put the random numbers from 1 to 10 as value of each index in the array a.

Let’s try to solve it first and check the answer below!

I divided the code into the setting part and the printing part. It’s not necessary, but it makes your code more readable. It will be needed when the code gets complicated.

That’s all for today guys! Let’s do some more practices about random class and the array tomorrow. It will be so much fun! Thank you for reading, see ya!

--

--

--

Happy coding!

Student Kim

Learn something new everyday.