Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Irregular Array

An irregular array, also known as a jagged array or a ragged array, is an array of arrays with different lengths. In Java, you can create and work with irregular arrays just like regular arrays, but with some extra considerations. This tutorial will walk you through the process of creating and manipulating irregular arrays in Java.

  • Declare an irregular array:

To declare an irregular array, you first need to specify the data type, followed by two sets of square brackets. The first set of brackets represents the outer array, and the second set represents the inner arrays.

int[][] irregularArray;
  • Create an irregular array:

To create an irregular array, you can use the new keyword to allocate memory for the outer array, and then allocate memory for each inner array individually.

irregularArray = new int[3][];
irregularArray[0] = new int[2];
irregularArray[1] = new int[4];
irregularArray[2] = new int[3];
  • Initialize an irregular array:

You can also initialize an irregular array directly using the following syntax:

int[][] irregularArray = {
    {1, 2},
    {3, 4, 5, 6},
    {7, 8, 9}
};
  • Access elements in an irregular array:

To access an element in an irregular array, you need to specify the indices of both the outer and inner arrays.

int value = irregularArray[1][2]; // value will be 5
  • Iterate through an irregular array:

You can use nested loops to iterate through an irregular array. The outer loop iterates through the outer array, and the inner loop iterates through the inner arrays.

for (int i = 0; i < irregularArray.length; i++) {
    for (int j = 0; j < irregularArray[i].length; j++) {
        System.out.print(irregularArray[i][j] + " ");
    }
    System.out.println();
}

This code will output:

1 2
3 4 5 6
7 8 9

That's it! You now know how to create, initialize, access, and iterate through irregular arrays in Java. You can use these concepts to solve problems that require multi-dimensional data structures with varying row lengths.

  1. Creating and initializing irregular arrays in Java

    // Creating a jagged array
    int[][] jaggedArray = new int[3][];
    
    // Initializing subarrays with different lengths
    jaggedArray[0] = new int[]{1, 2, 3};
    jaggedArray[1] = new int[]{4, 5, 6, 7};
    jaggedArray[2] = new int[]{8, 9};
    
  2. Accessing elements in a jagged array in Java

    // Accessing elements in a jagged array
    int value = jaggedArray[1][2]; // Accessing element in the second subarray at index 2
    
  3. Dynamic sizing of arrays within an array in Java

    Jagged arrays allow for dynamic sizing of subarrays, each with its own length.

  4. Initializing and working with arrays of different lengths in Java

    // Initializing arrays of different lengths
    int[][] jaggedArray = {
        {1, 2, 3},
        {4, 5, 6, 7},
        {8, 9}
    };
    
    // Working with arrays of different lengths
    for (int[] subarray : jaggedArray) {
        for (int element : subarray) {
            System.out.print(element + " ");
        }
        System.out.println();
    }
    
  5. Memory allocation for jagged arrays in Java

    Jagged arrays allocate memory for each subarray separately, providing flexibility in memory usage.

  6. Nested loops for iterating through a jagged array in Java

    // Nested loops for iterating through a jagged array
    for (int i = 0; i < jaggedArray.length; i++) {
        for (int j = 0; j < jaggedArray[i].length; j++) {
            System.out.print(jaggedArray[i][j] + " ");
        }
        System.out.println();
    }