0% found this document useful (0 votes)
7 views6 pages

Java 2D Array Syntax and Examples

Uploaded by

ankit.kec19
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Java 2D Array Syntax and Examples

Uploaded by

ankit.kec19
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Example :-01

__________________
int [][] arr=new int [2][];
arr[0]=new int [2];
arr[1]=new int [3];

Note:- Atleast base size we have to specified.


---------------------------------------------------------------------------
Que-1)
_______________
Which are the valid syntax for the creation of array?

* int[][] arr01=new int[3][2];

* int[][] arr02=new int[5][];

* int[][] arr03=new int[][];

* int[][][] arr04=new int[3][2][1];

* int[][][] arr05=new int[3][1][];

* int[][][] arr06=new int[3][][3];

* int[][][] arr07=new int[][2][3];

* int[][] arr08=new int[3][2][2];

* int [] arr09=new int[] {5,6};

* int [][] arr10=new int[][] {{1,2},{3,4}};

* int [][] arr11=new int[][] {{}};

* int [][] arr12=new int[][] {{0,0},{1,1}};

* int [][][] arr13=new int[][][] {{{5,5},{6,6},{7,7}}};


___________________________________________________________________________________
Que-2) What will be the output of this code?

* int [][] arr01=new int[2][2];


[Link]([Link]().getName());

* byte [][] arr02=new byte[2][3];


[Link]([Link]().getName());

* long [][] arr03=new long[1][1];


[Link]([Link]().getName());

* boolean [][][] arr04=new boolean[3][2][];


[Link]([Link]().getName());
___________________________________________________________________________________
Que-3) What will be the output of this program?

Note :- Draw the structure in digramatically format


and then think about the output :-

public class Program03 {


public static void main(String[] args) {
int [][] arr01=new int[2][3];
[Link](arr01);
[Link](arr01[0]);
[Link](arr01[1]);
[Link](arr01[0][0]);
[Link](arr01[0][1]);

}
}
___________________________________________________________________________________
_
Que-4) What will be the output of this program?

Note :- Draw the structure in digramatically format


and then think about the output :-

public class Program04 {


public static void main(String[] args) {
int [][] arr=new int[][] {{1,2,3},{4,5,6}};
[Link](arr[0][0]);
[Link](arr[0][1]);
[Link](arr[0][2]);
[Link](arr[1][0]);
[Link](arr[1][1]);
[Link](arr[1][2]);
}
}
________________________________________________________________
Lab Program
________________
public class Program02 {
public static void main(String[] args) {
// int arr[]=new int[size];
int arr[][]=new int[2][];
arr[0]=new int[3];
arr[1]=new int[2];
[Link]("arrays length"+[Link]);

/* 0 1
* [___________|_______________]
* / |
* / |
* / |
* 0 1 2 0 1
* [___|___|___] [___|___]
*
*
*/

int[][] arr01=new int[3][2];


int[][] arr02=new int[5][];
arr02[0]=new int[2];
arr02[1]=new int[1];
arr02[2]=new int[0];
arr02[3]=new int[4];
//int[][] arr03=new int[][];
//int[][] arr03=new int[][5];
int[][][] arr04=new int[3][2][1];

/* 0 1 2
* [___________|__________|___________]
* | | |
* 0 1 0 1 0
* [__|__] [__|__] [__|__]
* / | / \ | |
0 0 0 0 0 0
* [__] [__] [__] [__] [__] [_|_]
*
*/

int[][][] arr05=new int[3][1][];


arr05[0][0]=new int[3];
arr05[1][0]=new int[2];
arr05[2][0]=new int[1];
// arr05[0][1]=new int[3];
// arr05[1][1]=new int[2];
/* 0 1 2
* [_________|____________|__________]
* | | |
* 0 0 0
* [__] [__] [__]
* | \ \
* 0 1 2 0 1 0
* [___|____|___] [__|__] [__]
*
*/

// int[][][] arr06=new int[3][][3];

// int[][] arr08=new int[3][2][2];

int [][] arr10=new int[][] {{1,2},{3,4}};


[Link](arr10[0][1]);
[Link](arr10[1][1]);

/* 0 1
* [___________|____________]
* | \
* 0 1 0 1
* [ 1 | 2 ] [ 3| 4]
*/

int [][] arr11=new int[][] {{10}};


[Link]([Link]);
[Link](arr11[0][0]);

int arr12[]=new int [5];


[Link]([Link]().getName());
int arr13[][]=new int[4][];
[Link]([Link]().getName());

int arr14[][]=new int[2][];


[Link](arr14);
[Link](arr14[0]);
//[Link](arr14[0][0]);
int arr15[][]=new int[2][2];
[Link](arr15);
[Link](arr15[0]);
[Link](arr15[0][0]);

}
}
-----------------------------------------------------------------------------------
-----
Que-> Write a java program to print the 2D Arrays element with the help of for
loop ?

import [Link].*;
class GFG {
public static void main(String[] args)
{

int[][] arr = { { 1, 2 }, { 3, 4 } };

for (int i = 0; i < 2; i++)


for (int j = 0; j < 2; j++)
[Link]("arr[" + i + "][" + j + "] = "
+ arr[i][j]);
}
}

___________________________________________________________________________________
___________________
Que-2 > Write a java program to print the 2D array element in matrix format ?

import [Link].*;
class GFG {
public static void main(String[] args)
{

int[][] arr = { { 1, 2 }, { 3, 4 } };

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
[Link](arr[i][j] + " ");
}

[Link]();
}
}
}
___________________________________________________________________________________
____________________
Que-> Write a java program to print the array element of 2D array if the
base size is there but the next array size is not fixed

public class Program02 {


public static void main(String[] args) {
int [][]arr=new int[][] {{2,5,6},{1,2},{4}};
for( int i=0;i<[Link];i++) {
for(int j=0;j<arr[i].length;j++) {
[Link]("arr["+i+"]"+"arr["+j+"]="+arr[i][j]);
}
}
}
}
___________________________________________________________________________________
____________________
Que-3> Write a java program to read the row and columns from the user and then read
the element
from the user and print the element with the help of for loop in matrix
format?
___________________
public class TwoDArray {
public static void main(String[] args) {
int rows = 4;
int columns = 4;

int[][] array = new int[rows][columns];

int value = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = value;
value++;
}
}

[Link]("The 2D array is: ");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
[Link](array[i][j] + " ");
}
[Link]();
}
}
}
___________________________________________________________________________________
______________________

Que-4> Write a java program to print the 3d array element with the help of for
loop?
______________________
import [Link].*;
class GFG {
public static void main(String[] args)
{

int[][][] arr = { { { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } } };

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

for (int k = 0; k < 2; k++) {


[Link](arr[i][j][k] + " ");
}

[Link]();
}
[Link]();
}
}
}
___________________________________________________________________________________
_____________________

Common questions

Powered by AI

Attempting to access an uninitialized index like `System.out.println(arr14[0][0]);` results in a `NullPointerException` because the sub-array at index `[0]` has not been initialized. Handling this requires first checking if the sub-array is initialized using a conditional check or using try-catch to manage the exception gracefully .

Using `getClass().getName()` on arrays like `int[][] arr` helps explicitly identify the array's class type, which is crucial for debugging complex structures. For instance, `System.out.println(arr.getClass().getName());` showing `[I` for integers indicates the array's dimensions and base type. This aids in verifying expected structure and troubleshooting potential misconfigurations by confirming array type during runtime .

A nested for-loop printing elements like `for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) System.out.println(arr[i][j]);` produces sequential lines of elements from the 2D array, effectively stacking them in a row-column format. This is an effective way of accessing each element because it mirrors the array’s logical arrangement, maintaining row by row processing .

To print elements of a jagged array, you loop through each sub-array individually. For instance, given `int [][]arr=new int[][] {{2,5,6},{1,2},{4}};`, nested loops iterate the rows/sub-arrays first and then the elements within these sub-arrays. The output would be a sequential print of individual elements, like `2 5 6 1 2 4`, each accessed within their respective sub-array loops .

The Java syntax `int[][] arr03=new int[][];` would throw a compilation error because it attempts to instantiate an array without specifying an initial size for any dimension. In Java, the base size of the array must be specified at least during its initialization .

The valid syntax is `int[][] arr=new int[5][];`. Here, the primary array size is specified as 5, but the secondary sizes (the array objects within each slot of the primary array) are not defined at this point. This allows the creation of jagged arrays where each sub-array can be of a different size .

The length of a base array in a multidimensional structure is determined during declaration (`int arr[][]=new int[2][]; arr.length` is 2), but the capacities (second-dimension lengths) are flexible, set per row when initialized (`arr[0] = new int[3];`). This flexibility allows for jagged arrays where each sub-array can be of varying lengths, supporting diverse data sizes, but also requires careful handling to avoid `NullPointerException`s when accessing non-initialized elements .

To identify the class type of a two-dimensional byte array in Java, you can use the method `getClass().getName()`. When executed, `byte[][] arr02=new byte[2][3]; System.out.println(arr02.getClass().getName());` will output `[B;`, which signifies a two-dimensional byte array .

When a 2D array, like `int [][] arr01=new int[2][3];` is printed using `System.out.println(arr01);`, it will output a reference to the object in memory, not its contents. This indicates that Java arrays are objects in memory, and when printed without accessing specific elements, the address or reference ID is returned, which is a native way Java represents objects internally .

Using a program design like in the `TwoDArray` class, initialize the 2D array with specified dimensions. Utilize nested loops to prompt user input for each element. Example: `array[i][j] = scanner.nextInt();` within loops for `i` and `j`. The matrix output involves another set of nested loops to print each row's elements followed by a newline for next. This gives a matrix-like visual structure .

You might also like