DEV Community

Cover image for JAVA Basics #7 - Arrays
Chathumi Kumarapeli
Chathumi Kumarapeli

Posted on • Edited on

JAVA Basics #7 - Arrays

In this article we are going to learn about Arrays.

Arrays

alt 7_arrays1

Examples of arrays:
stdNames = {"Edward Cullen", "Jacob Black", "Isabella swan", "Alice Cullen"}
primeNumbers = {2, 3, 5, 7, 11, 13, 17, 19}
vowels = {'a', 'e', 'i', 'o', 'u'}
bookSeries = {"Twilight Saga", "Harry Potter", "Hunger Games", "After"}

Given above are few examples of arrays. Let's see how we can implement them in java.
First you need to know that we have to import a library named java.util.Arrays to perform array oppertaions. Without that you can not use Arrays in your program. After importing that library we can declare array variables as follows;

package com.company;
import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        int[] primeNumbers = new int[5];
        primeNumbers[0] = 2;
        primeNumbers[1] = 3;
        primeNumbers[2] = 5;
        System.out.println(primeNumbers[1]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Here line int[] primeNumbers = new int[5] is where we declare the array. Here we are declaring an array named primeNumbers which can store 5 elements in it. After that I have stored the numbers by calling the indexes. In the zeroth index I have stored the number 2. First index with number 3 and second index with number 5. When you call primeNumbers[1] it will return the number at the index '1' which is '3' and print it.

Task 01

Tryout these print statements with the given array and see the outputs. Reason why they give those outputs.

System.out.println(primeNumbers[4]);
System.out.println(primeNumbers[6]);
System.out.println(primeNumbers);
Enter fullscreen mode Exit fullscreen mode

I will reason the last print statement. You might have thought that System.out.println(primeNumbers) would print the elements of the arrays. However, your output would looks something like this '[I@1b6d3586'. What is this output? Any guesses?

Here, by default java returns a string which is calculated based on the address of the object 'primeNumbers' in the memory, when you called the System.out.println(primeNumbers) statement. Is there a way in which we can print the elements of the array? Yes, for that you have to use the predefined methods in the Arrays class.

System.out.println(Arrays.toString(primeNumbers));
Enter fullscreen mode Exit fullscreen mode

The toString() method will converts the array to a string and will give the output '[2, 3, 5, 0, 0]' as the result. Why do the last two elements are zeros? That is because we have not yet stores any values in 3rd and 4th indexes. How can you get the number of elements of a particular array?

System.out.println(primeNumbers.length);
Enter fullscreen mode Exit fullscreen mode

Above code gives the length of the array. Here it prints '5'. In other words it prints the size that we have allocated when we first initialized the array. It includes the last two zeros as well.

There's another way to initialize arrays.

int[] numbers = {2,3,5};
Enter fullscreen mode Exit fullscreen mode

The above code will create an array named numbers with size '3' and stores elements 2, 3, and 5 in it.

Just like in strings, Class Arrays also has many predefined methods. I will leave them for you to explore and practice ;)

Task 02

Given an array evenNumbers = {4, 2, 10, 200, 144, 50, 24, 96} print the

  1. maximum number
  2. minimum number
  3. summation of all the numbers
  4. average of all the numbers

Multi-dimensional Arrays

Arrays may not always be one dimensional. You also may want to declare two dimensional arrays (like matrices). Let's see how we can do that.

int[][] matrix = new int[2][3]; // line 1
matrix[0][0] = 1; // line 2
matrix[0][1] = 2; // line 3
matrix[1][2] = 3; // line 4
System.out.println(Arrays.deepToString(matrix)); // line 5
Enter fullscreen mode Exit fullscreen mode

In 'line 1' we have created an array object named 'matrix' which is of integer type. We have given int[2][3] as the size. So this creates a 2x3 matrix (2 rows and 3 columns).
In line 2 we have stored the value '1' in the place of [0][0]. You know indexing does not start with number '1'. So this 00 represent the joining position of first row first column.
The numbers 3 and 4 have also been stored in the matrix and the elements of the matrix are printed by the line 5 with the use of 'Arrays' class.
The output will look like;
[[1, 2, 0], [0, 0, 3]]

The matrix can be displayed as follows in a tabular form.

Row index\Column index 0 1 2
0 1 2 0
1 0 0 3

Just like one dimensional arrays, 2D arrays can also be declared as follows.

int[][] matrixTwo = { { 1, 2, 3 }, { 4, 5, 6 } };
System.out.println(Arrays.deepToString(matrixTwo));
Enter fullscreen mode Exit fullscreen mode

This code generates a 3x3 matrix which is [[1, 2, 3], [4, 5, 6]].

With that we can wrap up this article. Let's learn more about java in upcoming articles :)

Top comments (0)