Java Array Tutorial – Single & Multi Dimensional Arrays In Java

Last updated on May 22,2019 40.3K Views

Java Array Tutorial – Single & Multi Dimensional Arrays In Java

edureka.co

In the previous blog, you have learned about Java string. Through this blog on Java Array, I will explain you the concepts of Arrays in Java and how single & multi-dimensional arrays work. Learning about Java arrays is essential in earning your java certification.

In this Java Array blog, I would be covering the following topics:

Before we proceed further, let’s see why exactly we need Java Array:

Now, let’s begin with this post on Java Array and understand what exactly are arrays.

What are Java Arrays?

Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store one or more values of a specific data type and provide indexed access to store the same. A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information.

Obtaining an array is a two-step process.

So, let us see how can we declare arrays in different ways.

General Form of Java Array Initialization

  

 

Example:- int month_days[]; 

 

General Form of Java Array Initialization

Example:- 

Arrays can be initialized when they are declared. The array will automatically be created large enough to hold the number of elements you specify in the array initializer. There is no need to use new.Now, let us see how we can implement this.

General Form of Java Array Initialization

The following code creates an initialized array of integers:


class MyArray{

public static voide main(String args[]){

int month_days[ ] = {31,28,31,30,31,30,31,30,31,30,31};

System.out.println("April has " + month+days[3] + "days.");

}

}

It will only be fair if I explain how you can access elements in a Java Array.

Accessing a Specific Element in a Java Array

In arrays, we can access the specific element by its index within square brackets.

Example:- 

Putting together all the pieces,

public static void main(String args[]) {
   int month_days[];
    month_days = new int[12];
    month_days[0] = 31;
    month_days[1] = 28;
    month_days[2] = 31;
    month_days[3] = 30;
    month_days[4] = 31;
    month_days[5] = 30;
    month_days[6] = 31;
    month_days[8] = 30;
    month_days[9] = 31;
    month_days[10] = 30;
    month_days[11] = 31;
     System.out.println("April has " + month_days[3] + " days.");
     }
}

So, this was all about the arrays and its declaration and how single dimension arrays can be used.

What if I tell you, there can be an array inside an array. I know it sounds a bit complex, but don’t worry, I know how to make it easy for you.

Java Multidimensional Array

Multidimensional arrays are arrays of arrays.

Declaring Multidimensional Array

To declare it, we have to specify each additional index using another set of square brackets.

Conceptually, the array declared above would be represented as shown in the figure:-

Let us now Demonstrate Multidimensional Array.

The following program, numbers each element in the array from left to right, top to bottom, and then displays these values:

class Mul2D{
 public static void main(String args[]) {
       int mul2d[][]= new int[4][5];
        int i, j, k = 0;
     for(i=0; i<4; i++)
       for(j=0; j<5; j++) {
       Mul2D[i][j] = k;
       k++;
}
for(i=0; i<4; i++) {
    for(j=0; j<5; j++);
       System.out.print(mul2d[i][j] + " ");
       System.out.println();
       }
   }
}

This program generates the following output:

 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 

These are other Multidimensional arrays representation of other data types.

So, this was all about the Multidimensional Arrays. Now, Let us see, how to pass an array to a method as a parameter like the other data types.

Passing Java Array to a Method

We can also pass arrays to methods just as we can pass primitive type values to methods.

Example:- 

public class PMethods{
public static void display(int y[])
     {
             System.out.println(y[0]);
             System.out.println(y[1]);
             System.out.println(y[2]);

     }
public static void main(String args[])
     {
     int x[] = { 1, 2, 3 };
     display(x);
     }
}

This will be the output of the program

1

2

3

This brings us to the end of Java Array blog. I hope you have enjoyed this post on Java Array. If you are looking for in-depth knowledge of Java, do read Java Tutorial blog where you will be explained in detail on below topics with examples.

You can also learn Java through our YouTube Java Tutorial playlist. Happy Learning!! 

If you found this blog on “Java Array” useful, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Got a question for us? Please mention it in the comments section and we will get back to you.

Got a question for us? Please mention it in the comments section and we will get back to you.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 1st June,2024

1st June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial