Skip to content

Latest commit

 

History

History
224 lines (149 loc) · 5.46 KB

File metadata and controls

224 lines (149 loc) · 5.46 KB

Arrays in Java

Introduction

Arrays in Java are objects that store multiple variables of the same type. They provide a convenient way to handle large amounts of data of a similar nature. This guide will cover the basics of arrays, as well as more advanced concepts and operations.

Basics of Arrays

Declaration and Initialization

One-Dimensional Arrays

A one-dimensional array is like a list of variables. The syntax for declaring an array is:

int[] arrayName;  // Declares an array of integers

To initialize the array, you can use the new keyword:

arrayName = new int[10];  // Initializes the array with a size of 10

Alternatively, you can declare and initialize the array in one step:

int[] arrayName = new int[10];
int[] numbers = {90, 85, 88, 92, 78};  // Declaration and initialization

Accessing Elements

Elements in an array are accessed using their index, with the first element having an index of 0:

int firstElement = numbers[0];  // Accesses the first element of the array
numbers[1] = 86;  // Modifies the second element of the array

Length of an Array

The length of an array is accessed using the length property:

int arrayLength = numbers.length;  // Returns the length of the array

Looping Through Arrays

You can use a for loop to iterate over the elements of an array:

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

Printing Arrays

Using System.out.println to print an array directly will output a hash code. To print the contents, use Arrays.toString():

import java.util.Arrays;

System.out.println(Arrays.toString(numbers));  // Output: [90, 85, 88, 92, 78]

Two-Dimensional Arrays

Two-dimensional arrays are like tables with rows and columns. They are arrays of arrays.

Declaration and Initialization

int[][] arrayName = new int[3][4];  // 3 rows and 4 columns

Alternatively, you can initialize it directly:

int[][] marks = {
    {90, 85, 88, 92},
    {78, 88, 85, 90}
};

Accessing Elements

Access elements using two indices: one for the row and one for the column:

int firstElement = marks[0][0];  // Accesses the first element in the first row
marks[1][2] = 95;  // Modifies the third element in the second row

Looping Through Two-Dimensional Arrays

Use nested for loops to iterate through elements:

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

Printing Two-Dimensional Arrays

To print the entire 2D array, use Arrays.deepToString():

System.out.println(Arrays.deepToString(marks));  // Output: [[90, 85, 88, 92], [78, 88, 85, 90]]

Common Array Operations

Sorting Arrays

Use Arrays.sort() to sort an array in ascending order:

Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));  // Output: [78, 85, 88, 90, 92]

Copying Arrays

Use Arrays.copyOf() to copy an array:

int[] copiedArray = Arrays.copyOf(numbers, numbers.length);
System.out.println(Arrays.toString(copiedArray));

Filling Arrays

Use Arrays.fill() to fill an array with a specific value:

int[] filledArray = new int[5];
Arrays.fill(filledArray, 1);
System.out.println(Arrays.toString(filledArray));  // Output: [1, 1, 1, 1, 1]

Searching Arrays

Use Arrays.binarySearch() to search for a value in a sorted array:

int index = Arrays.binarySearch(numbers, 88);
System.out.println("Index of 88: " + index);  // Output: Index of 88: 2

Multi-Dimensional Arrays (Beyond 2D)

Java supports arrays of more than two dimensions. The concept is similar to 2D arrays but with more indices:

int[][][] threeDArray = new int[3][4][2];  // 3x4x2 array

Access elements similarly by adding more indices:

threeDArray[0][1][1] = 5;

Memory Considerations

Default Values

Uninitialized array elements have default values based on their type:

  • int, byte, short, long: 0
  • float, double: 0.0
  • char: '\u0000'
  • boolean: false
  • Objects: null

Jagged Arrays

Java allows jagged arrays (arrays of arrays with different lengths):

int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[2];
jaggedArray[1] = new int[3];
jaggedArray[2] = new int[1];

Advanced Topics

ArrayLists

While arrays have a fixed size, ArrayList from the java.util package provides a dynamic array that can grow as needed:

import java.util.ArrayList;

ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(10);
arrayList.add(20);
System.out.println(arrayList);  // Output: [10, 20]

Using Arrays with Generics

Generics allow you to create classes, interfaces, and methods with a placeholder for types:

import java.util.ArrayList;

ArrayList<String> stringList = new ArrayList<>();
stringList.add("Hello");
stringList.add("World");
System.out.println(stringList);  // Output: [Hello, World]

Conclusion

Arrays in Java provide a powerful and efficient way to handle multiple data elements. From basic one-dimensional arrays to more complex multi-dimensional arrays and dynamic ArrayList, understanding and utilizing arrays effectively is crucial for Java programming. This guide covers the essentials and advanced topics, making it a comprehensive reference for working with arrays in Java.