Java 2D Array Example - Accessing Elements (With Code)
This Snippet is coded by a user inOnline java Compiler. You can see and run this code too.
زبان: Java
This Java code demonstrates how to work with a two-dimensional array. A 2D array is essentially an array of arrays, allowing you to store data in a grid-like structure.
- An integer array
myNumbersis declared with two rows: the first row contains{1, 2, 3, 4}and the second row contains{5, 6, 7}. - Rows and columns are zero-indexed. So
myNumbers[1][2]refers to the third element (index 2) of the second row (index 1). - The value at that position is 7.
- Finally,
System.out.println(x)prints the value to the console.
This example is a great starting point for understanding how to navigate and access data in multi-dimensional arrays in Java.