Initialize 2D array in Java

In this article, we will learn to initialize 2D array in Java.

The array is a data structure that is used to collect a similar type of data into contiguous memory space. An array can be a single-dimensional or multidimensional. An array that has 2 dimensions is called 2D or two-dimensional array.
There are several ways to create and initialize a 2D array in Java. Let’s see some examples.

Initialize 2D array Using for loop

This is the simplest approach in which we create an array and initialize every index using for loop.

Output:

0 1
2 3
4 5

Initialize 2D array using an initializer

We can use an initializer block to initialize the array at the same time while creating an array. See the example below.

Output

0 1
2 3
4 5

Initialize 2D array of variable columns length

If we want to create an array that contains variable length variable length columns then use the below approach and initialize it using the for loop. See the example below.

Output

0 1 2
3 4 5 6
7 8 9 10 11

Initialize 2D array with heterogeneous data

Although array is used to collect similar type of data but we can store different type of values as well. See the example below.

Output

0 1 2
A A A A
3.0 3.0 3.0 3.0 3.0

Initialize 2D array using initialilzer with columns

If we want to initialize a variable lenght columns array at the same time of creating an array then use initialilzer block as we did in the given example.

Output

1 2 3
4 5 6 7
8 9 10 11 12

Initialize 2D array using Reflection API

Java provides a class Array in reflection package that can be used to create an array. Here, newInstance() method is used to create an instance of array which is later initialized in the for loop.

Output

0 1
2 3
4 5

Initialize 2D array using the toCharArray()

If we have a character array and initialize it at the same time then we can use toCharArray() method of String class that returns a char array. See the example below.

Output:

J A V A
P Y T H O N
R E S T

That’s all about How to initialize 2D array in java

Related Posts

  • Set an array to another array in java
    23 April

    Set an Array Equal to Another Array in Java

    Table of ContentsSetting an Array Variable Equal to Another Array VariableSet an Array Equal to Another Array in Java Using the clone() MethodSet an Array Equal to Another Array in Java Using the arraycopy() MethodSet an Array Equal to Another Array in Java Using the copyOf() MethodSet an Array Equal to Another Array in Java […]

  • Check if array is empty in Java
    04 March

    Check if Array Is Empty in Java

    Table of ContentsCheck if the Array Is Empty in JavaThe Array Variable Has the Null ReferenceThe Array Does Not Contain Any ElementThe Array Has Only Null ElementsUsing the Java Library to Check if the Array Is Empty in JavaUsing Apache Commons Library to Check if the Array Is Empty in JavaConclusion In this post, we […]

  • Initialize empty array in java
    01 January

    Initialize empty array in java

    Table of ContentsIntroductionHow do you initialize an empty array in java?Using new Keyword with predefined Values and SizeUsing Anonymous Array Objects to initialize empty arrayUsing java.util.Scanner Class for user input with predefined size. Using java.io.BufferedReader to initialize array for user input with unknown sizeUsing fill() method of java.util.Arrays Class to initialize empty array 💡 Outline You […]

  • Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers
    07 April

    Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers

    In this article, we look at a problem : Given an Array of Positive Integers, Find the [Maximum Difference](https://java2blog.com/maximum-difference-between-two-elements-in-array/ "Maximum Difference") between Two Adjacent Numbers. For each pair of elements we need to compute their difference and find the Maximum value of all the differences in array. Let us look at an example, Consider this […]

  • 11 November

    How to print array in java

    Learn about how to print array in java in multiple ways.

  • 24 October

    Java add to array

    Table of ContentsUsing Apache’s common lang libraryBy writing your own utility method In this post, we will see how to add elements to the array. Using Apache’s common lang library You can use varargs add method to add elements to array dynamically. Here are the few add overloaded methods provided by ArrayUtils class If you […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.