Flat Preloader Icon

2D Array

About 2D Array

A 2D array, also known as a two-dimensional array, is a data structure that represents a collection of elements organized in a grid-like fashion, consisting of rows and columns. It is essentially an array of arrays, where each element can be accessed using two indices: one for the row and another for the column. 2D arrays are commonly used to store and manipulate data that has a natural two-dimensional structure, such as matrices, tables, grids, and images.

Basic introduction to 2D arrays in data structures:

  1. Structure and Declaration:

    • A 2D array is declared in most programming languages using square brackets for both dimensions, like int myArray[row][column] or type arrayName[row][column].
    • It’s essential to specify the number of rows and columns when declaring a 2D array, which determines its size.
  2. Accessing Elements:

    • Elements in a 2D array are accessed using two indices, one for the row and one for the column. For example, myArray[i][j] refers to the element in the i-th row and j-th column.
  3. Initialization:

    • You can initialize a 2D array by providing values for each element during declaration or by using loops to assign values later.
  4. Traversal:

    • To work with the elements of a 2D array, nested loops are often used to traverse through the rows and columns.
  5. Common Use Cases:

    • 2D arrays are commonly used in applications where data has a grid-like structure. Some examples include representing matrices in linear algebra, tables in databases, images in computer graphics, game boards, and more.
  6. Memory Allocation:

    • The memory for a 2D array is allocated contiguously in memory. The size of the array is the product of the number of rows and columns multiplied by the size of each element.
  7. Dynamic 2D Arrays:

    • In some cases, you might need to work with dynamically sized 2D arrays. In languages like C or C++, you can allocate memory for these arrays at runtime using pointers or dynamic memory allocation functions (e.g., malloc).
  8. Complexity:

    • Accessing an element in a 2D array is typically done in constant time O(1), assuming you know the indices. However, searching, sorting, or performing operations on 2D arrays can have time complexities related to the specific algorithms used.

How To Declare 2D Array

				
					data_type array_name
[row_size][column_size];

				
			

Java:

In Java, you can declare a 2D array using a similar syntax:

				
					data_type[][] array_name
= new data_type[row_size]
[column_size];

				
			

Mapping 2D Array To 1D Array

In Java, you can map a 2D array to a 1D array by iterating through the elements of the 2D array and storing them in a 1D array. The mapping depends on how you want to flatten the 2D array into a 1D array (e.g., row-wise or column-wise). Here’s an example of mapping a 2D array to a 1D array in both row-wise and column-wise fashion:

				
					public class ArrayMapping {
public static void main(String[] args) {
        int[][] twoDArray = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        // Map to 1D array (row-wise)
        int[] rowWiseArray 
        = mapTo1DRowWise(twoDArray);
        System.out.println
        
        ("Row-wise mapping: " 
        + java.util.Arrays.toString
        );

        // Map to 1D array (column-wise)
        int[] columnWiseArray = 
        mapTo1DColumnWise(twoDArray);
        System.out.println
        ("Column-wise mapping: " +
        java.util.Arrays.toString
        (columnWiseArray));
    }

    // Map 2D array to 1D array (row-wise)
    public static int[]
    
    mapTo1DRowWise(int[][] twoDArray) {
        int rows = twoDArray.length;
        int cols = twoDArray[0].length;
        int[] oneDArray = new int[rows * cols];

        int index = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
    oneDArray[index++] = twoDArray[i][j];
            }
        }

        return oneDArray;
    }

    // Map 2D array to 1D array (column-wise)
    public static int[]
    mapTo1DColumnWise(int[][] twoDArray) {
        int rows = twoDArray.length;
        int cols = twoDArray[0].length;
        int[] oneDArray = new int[rows * cols];

        int index = 0;
        for (int j = 0; j < cols; j++) {
            for (int i = 0; i < rows; i++) {
        oneDArray[index++] = twoDArray[i][j];
            }
        }

        return oneDArray;
    }
}