Arrays in Java

Arrays are a way to store a list of items in java programming. Each element of the array holds an individual item. We can place items into and remove items from those slots as we need to. Arrays can contain any type of value, but we can’t store different types in a single array. We can have an array of integers, or an array of strings, or an array of arrays, but we can’t have an array that contains, for example, both strings and integers.

Three steps to create an array:

  1. Declare a variable to hold the array.
  2. Create a new array object and assign it to the variable.
  3. Store the array elements.

How to declare array variables?

The first step to creating an array is creating a variable that will hold the array. Array variables indicate the type of object the array will hold and the name of the array.

Examples:

String studentsNames[];
int studentsAge[];

// these two declarations could be written like this
String[] studentsNames;
int[] studentsAge;

Create a new array object

Then we may create an array object and assign it to that variable. There are two ways to,

  • Use new
  • Directly initialising the contents of that array
String[] student_names = new String[5];

That line creates a new array of Strings with five elements. When we create the new array object using new, we must indicate how many elements that array will hold.

Array objects can contain integers or booleans, just as they can contain objects:

int[] numbers = new int[30];

When we create an array object using a new need to initialized all elements. We can also create and initialize an array same time. Each of the elements inside the bracket must be of the same type and must be the same type as the variable that holds that array. An array of the size of the number of elements we have included will be automatically created.

String[] Countries = {"United state", "India", "Sri Lanka", "England", "South Africa"};

Example 1 : Write a programme for find max number in array?

public class Solution {
    public static void main(String args[]) {
        int[] Collection = {14, 45, 87, 12, 26, 91, 07}; //create array
        int max_num = 0;
 
        for(int i=0; i<Collection.length; i++) {
            if(Collection[i] > max_num) {
                max_num = Collection[i]; //Array max element save as max_num
            }
        }
        System.out.println("Collection Max number is "+max_num);
    }
}

Multidimensional Arrays in Java

Java not supported to the create multidimensional arrays, but we can create array of arrays in java programming.

int metrics[][] = new int[3][3];
metrics[0][0] = 2;
metrics[0][1] = 3;

Print 2D Arrays or Matrix in Java

public class Solution {
    public static void main(String args[]) {
        int[][] matrix = {{ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }};
        
        for(int i=0; i <matrix.length; i++) {
            for(int j=0; j<matrix.length; j++) {
                System.out.print(matrix[i][j]+ " ");
            }
            System.out.println();
        }
    }
}

Java is a popular programming language and widely-used programming language for many years. You can learn java programming with examples by following our Java articles sequentially.

 500 total views,  3 views today

2 thoughts on “Arrays in Java”

  1. Pingback: Data types in Java with examples | EasyFix Technology

Leave a Comment

Your email address will not be published. Required fields are marked *

wpChatIcon