Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Java Array Tutorial – Single & Multi Dimensional Arrays In Java

Last updated on May 22,2019 40.8K Views

53 / 72 Blog from Java Core Concepts

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:

  • Arrays are an important structure to hold data.
  • Java allows us to hold many objects of the same type using arrays.
  • It can be used with the help of a loop to access the elements by their index.

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.

Array - Java array - edurekaObtaining an array is a two-step process.

  • First, you must declare a variable of the desired array type
  • Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable

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

General Form of Java Array Initialization

  

 

Java Array General form - Java array - edurekaExample:- int month_days[]; 

 

General Form of Java Array Initialization

Java Array General form - Java array - edurekaExample:- 

Array Declaration Example - Java array - edureka

Array Declaration Example - Java array - edurekaArrays 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

Java Array General form - Java array - edureka

Array Declaration Example - Java array - edurekaThe 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:- 

Array Element Access - Java array - edureka

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.

2D array Declaration - Java array - edurekaConceptually, the array declared above would be represented as shown in the figure:-

2D Array - Java array - edurekaLet 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.

2D array Representation - Java array - edureka

2D array Representation - Java array - edureka

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.

  • Data Types and Operations in Java
  • Control Statements
  • Classes & Objects
  • Arrays
  • Basic OOPS Concept 

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 Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
Comments
1 Comment
  • Thanks edureka for all your efforts to making up this tutorial very very definite and obvious. My sister started a Java course last week it seems. She has a doubt in array concept. So I am searching for the array related article I found your article. Which is really very detailed with example diagrams. Once again thank you for this excellent article.

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Java Array Tutorial – Single & Multi Dimensional Arrays In Java

edureka.co