Array Handling in Java

                                Array handling in java

In Java, arrays are a type of data structure that can store a fixed-size sequential collection of elements of the same type. Arrays in Java can be of primitive data types, such as int, double, and char, or reference data types, such as objects and strings.

Here are some basic operations that can be performed on arrays in Java:

Declaration: To declare an array in Java, you need to specify the type of elements the array will store, followed by the array's name and square brackets [].

int[] numbers; // declaring an array of integers

String[] names; // declaring an array of strings

Allocation: To allocate memory for an array, you can use the new operator followed by the type of elements the array will store, followed by the size of the array in square brackets [].

numbers = new int[5]; // allocating memory for 5 integers

names = new String[3]; // allocating memory for 3 strings

Initialization: You can initialize an array when you declare it by including the values inside curly braces {}.

int[] numbers = {1, 2, 3, 4, 5}; // initializing an array of integers

String[] names = {"John", "Jane", "Jim"}; // initializing an array of strings

Accessing elements: To access an element in an array, you can use the array's name followed by the index of the element in square brackets []. Note that the index of the first element in an array is 0.

System.out.println(numbers[0]); // prints 1

System.out.println(names[1]); // prints "Jane"

Updating elements: To update an element in an array, you can simply assign a new value to it.

numbers[0] = 10;

names[1] = "Habbit2Code";

Looping through an array/Printing Array element: To loop through an array, you can use a for loop.

for (int i = 0; i < numbers.length; i++) {

    System.out.println(numbers[i]);

}


for (String name : names) {

    System.out.println(name);

}

These are just some of the basic operations you can perform on arrays in Java. There are many other useful array methods available in Java, such as sort(), copyOf(), and fill(), which you can use to manipulate arrays in various ways.

Methods in Array :

In Java, the java.util.Arrays class provides a variety of methods for working with arrays. Some of the most commonly used methods are:

sort(int[] a): Sorts the specified array of integers into ascending numerical order.

sort(double[] a): Sorts the specified array of doubles into ascending numerical order.

sort(char[] a): Sorts the specified array of characters into ascending order according to their Unicode values.

sort(String[] a): Sorts the specified array of strings into lexicographic (dictionary) order.

binarySearch(int[] a, int key): Searches the specified array of integers for the specified value using the binary search algorithm.

binarySearch(double[] a, double key): Searches the specified array of doubles for the specified value using the binary search algorithm.

binarySearch(char[] a, char key): Searches the specified array of characters for the specified value using the binary search algorithm.

binarySearch(String[] a, String key): Searches the specified array of strings for the specified value using the binary search algorithm.

fill(int[] a, int val): Assigns the specified int value to each element of the specified array of integers.

fill(double[] a, double val): Assigns the specified double value to each element of the specified array of doubles.

fill(char[] a, char val): Assigns the specified char value to each element of the specified array of characters.

copyOf(int[] original, int newLength): Returns a copy of the specified array of integers, truncated or padded with zeros (if necessary) so the copy has the specified length.

copyOf(double[] original, int newLength): Returns a copy of the specified array of doubles, truncated or padded with zeros (if necessary) so the copy has the specified length.

copyOf(char[] original, int newLength): Returns a copy of the specified array of characters, truncated or padded with the null character (if necessary) so the copy has the specified length.

copyOfRange(int[] original, int from, int to): Returns a copy of the specified range of the specified array of integers.

copyOfRange(double[] original, int from, int to): Returns a copy of the specified range of the specified array of doubles.

copyOfRange(char[] original, int from, int to): Returns a copy of the specified range of the specified array of characters.

toString(int[] a): Returns a string representation of the contents of the specified array of integers.

toString(double[] a): Returns a string representation of the contents of the specified array of doubles.

toString(char[] a): Returns a string representation of the contents of the specified array of characters.

These are some of the most commonly used methods of the java.util.Arrays class. You can refer to the Java documentation for a complete list of methods and their descriptions.

Comments