Arrays in Java

An array is a collection of elements of fixed size and type, for example, a collection of 10 elements of type int. It is a data structure to group elements. In this lesson, we will learn how to work with arrays in Java. Arrays in Java use an index to access an element.

Vectors are like growable arrays, but arrays have a fixed size. You cannot resize an array after creation. If you try, then an ArrayOutOfBoundsException will be thrown.

Here, we will discuss one-dimensional and multidimensional arrays,

One-Dimensional Arrays

Arrays with a single dimension are one-dimensional arrays in Java. Let us see how to declare 1D arrays in Java:

Declaration and Initialization

For declaring one-dimensional array in Java Programming, you need to specify the datatype and the variable name like the following,

datatype variable_name[];

Above, datatype is the type of data you want the elements to be
variable_name is the name of the variable

The following example shows the correct way of declaring arrays in Java,

int marks;

Above is an array of int. Here, we saw how to declare an array variable, with our variable name, marks. To create an array in Java and initialize it, use the new operator,

variable_name = new datatype[size];

Above,

Using new to allocate an array, specify the type and how many elements you want.
variable_name: name of the variable
datatype[size]: creates a new array

One-Dimensional Arrays – Create, Declare, & Initialize

Let us see different ways through which we can create, declare, and initialize arrays in Java:

Method 1: Creating an array using the new operator:

int[] marks; marks = new int[] {50, 89, 55, 95, 99, 87, 67, 85, 98, 90};

Method 2: Creating a new array:

Creating and initializing an array without using the new keyword

int[] marks = {50, 89, 55, 95, 99, 87, 67, 85, 98, 90};

Method 3: Creating a new array:

int[] marks = new int[10];

// Assigning values
marks[0] = 50;
marks[1] = 89;
marks[2] = 55;
marks[3] = 95;
marks[4] = 99;
marks[5] = 87;
marks[6] = 67;
marks[7] = 85;
marks[8] = 98;
marks[9] = 90; 

We also saw how to assign value to the elements. The indexes in the array start with 0.

Right now, we have space for 10 elements.
Add the index in the square brackets and insert a value to the specified element, like the following, which assigns the value 50 to the first element.

It assigns the value 89 to the second element, and the value 55 to the third element:

Let us see what we have done till now,

One-Dimensional-Array-in-Java Example

Here is a representation of the same array with different values:

One-Dimensional Array in Java

As we saw above, add all the elements and run the entire example below,

First Array Program in Java

Showing the usage of a one-dimensional array to add marks of 10 students. Here, we have shown multiple ways to create and initialize an array:

// One-Dimensional (1D) Array in Java

class Demo {
    public static void main(String[] args) {

        int result = 0;

        /* Method1: Creating a new array
        int[] marks;
        marks = new int[] {50, 89, 55, 95, 99, 87, 67, 85, 98, 90};
         */

        // Method2: Creating a new array
        // Creating and initializing an array without using new keyword
        int[] marks = {50, 89, 55, 95, 99, 87, 67, 85, 98, 90};

        /* Method3: Creating a new array
       int[] marks = new int[10];
        // Assigning values
        marks[0] = 50;
        marks[1] = 89;
        marks[2] = 55;
        marks[3] = 95;
        marks[4] = 99;
        marks[5] = 87;
        marks[6] = 67;
        marks[7] = 85;
        marks[8] = 98;
        marks[9] = 90; */

        // Error: java.lang.ArrayIndexOutOfBoundsException
        // Error: Index 11 out of bounds for length 10
        // marks[11] = 20;
        
        // Display the array
        System.out.println("Marks of all the 10 Students:");

        for (int i = 0; i < marks.length; i++) {
           System.out.println("Student " + (i + 1) + ": " + marks[i]);
        }

        // Sum of marks
        System.out.println("\nSum of Marks:");
        for (int i = 0; i < marks.length; i++) {
            result = result + marks[i]; // result+=marks[i]
        }
        System.out.println(result);
    }
} 

The following is the output of the above example,

Marks of all the 10 Students:
Student 1: 50
Student 2: 89
Student 3: 55
Student 4: 95
Student 5: 99
Student 6: 87
Student 7: 67
Student 8: 85
Student 9: 98
Student 10: 90

Sum of Marks:
815

Get the max and min from an array

Let us see an example to get the maximum and minimum from an array in Java:

// Get the max and min from an array

class Demo {
    public static void main(String[] args) {

        int result = 0;
        int[] marks = {50, 89, 40, 95, 99};

        int min_val = marks[0];
        int max_val = marks[0];

        for (int i = 0; i < marks.length; i++) {
            if (marks[i] < min_val ) {
                min_val = marks[i];
            }
        }

        System.out.println("Minimum = "+min_val);
        System.out.println("Maxmimum = "+max_val);
    }
}

Output

Minimum = 40
Maximum = 99

In this lesson, we learned about arrays in Java and how to work with one-dimensional arrays. Multi-dimensional arrays are discussed in the next lesson.

Loops in Java
Multidimensional Arrays in Java
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment