Java

Arrays

Java / Arrays

Arrays

Introduction

An array in Java is a collection of elements of the same data type stored in a single variable. Each element in the array is accessed using an index number. Arrays are used to store multiple values in one variable instead of creating separate variables for each value.

Properties of Array 

  1. Fixed Size
    Once an array is created, its size cannot be changed.
  2. Same Data Type
    All elements in an array must be of the same data type (e.g., int, String).
  3. Index Based
    Each element is accessed using an index number starting from 0.
  4. Contiguous Memory Allocation
    Array elements are stored in continuous memory locations.
  5. Object in Java
    Arrays are treated as objects in Java.
  6. Random Access
    Any element can be accessed directly using its index.

Example

public class Main {
   public static void main(String[] args) {

       // declaration and initialization of array
       int numbers[] = {10, 20, 30, 40, 50};

       // accessing array elements
       System.out.println("First Element: " + numbers[0]);
       System.out.println("Second Element: " + numbers[1]);
       System.out.println("Third Element: " + numbers[2]);

       // printing all elements using loop
       for (int i = 0; i < numbers.length; i++) {
           System.out.println("Element at index " + i + ": " + numbers[i]);
       }
   }
}

Advantages of Array

  1. Easy Access of Data
    You can access any element quickly using index numbers.
  2. Code Optimization
    Reduces the need to declare multiple variables.
  3. Easy to Use Loops
    Arrays work well with loops for processing multiple values.
  4. Better Memory Management
    Stores multiple values in a single variable efficiently.
  5. Fast Performance
    Direct indexing makes data access very fast.
  6. Supports Sorting and Searching
    Arrays are widely used in sorting and searching algorithms.

 

Multi-Dimensional Array in Java

A multi-dimensional array in Java is an array that contains other arrays. It is mainly used to store data in the form of rows and columns, just like a table or matrix. It is also called an array of arrays.

  • A multi-dimensional array is an array of arrays
  • It stores data in rows and columns format
  • It uses two indexes to access elements
  • Indexing always starts from 0

Example

public class Main {
   public static void main(String[] args) {

       int[][] numbers = {
           {1, 2, 3},
           {4, 5, 6}
       };

       // Accessing elements
       System.out.println(numbers[0][0]); // 1
       System.out.println(numbers[0][2]); // 3
       System.out.println(numbers[1][1]); // 5
       System.out.println(numbers[1][2]); // 6
   }
}

Disadvantages of Array

  1. Fixed Size
    The size of an array is fixed. Once created, it cannot be increased or decreased.
  2. Memory Wastage
    If the array size is larger than needed, unused memory gets wasted.
  3. Same Data Type Only
    Arrays can store only one type of data (e.g., all int or all String).
  4. Insertion and Deletion is Difficult
    Adding or removing elements in an array is not easy and takes more time.
  5. No Built-in Features
    Arrays do not provide advanced features like searching, sorting, or dynamic resizing.
  6. Risk of Index Errors
    If you access an invalid index, it causes an error (ArrayIndexOutOfBoundsException).
Technology
Java
want to connect with us ?
Contact Us