Java Array Methods

In Java, arrays themselves are not objects with built-in methods, but you can use methods from utility classes like java.util.Arrays to manipulate arrays. Here's a list of basic operations and common methods used with arrays in Java:

Method

Description

Example

Returns the number of elements in the array

int[] arr = {10, 20, 30, 40, 50};
System.out.println(arr.length); // Output: 5

Converts an array to a string.

import java.util.Arrays;

int[] arr = {10, 20, 30};
System.out.println(Arrays.toString(arr)); // Output: [10, 20, 30]

Searches for a key in a sorted array.

import java.util.Arrays;

int[] arr = {10, 20, 30, 40};
int index = Arrays.binarySearch(arr, 30);
System.out.println(index); // Output: 2

Compares two arrays for equality.

import java.util.Arrays;

int[] arr1 = {10, 20, 30};
int[] arr2 = {10, 20, 30};
System.out.println(Arrays.equals(arr1, arr2)); // Output: true

Compares two multi-dimensional arrays for equality.

import java.util.Arrays;

int[][] arr1 = {{1, 2}, {3, 4}};
int[][] arr2 = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepEquals(arr1, arr2)); // Output: true

Fills an array with a specific value.

import java.util.Arrays;

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

Sorts an array in ascending order.

import java.util.Arrays;

int[] arr = {30, 10, 20, 50};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // Output: [10, 20, 30, 50]

Copies an array to a new array.

import java.util.Arrays;

int[] original = {1, 2, 3};
int[] copy = Arrays.copyOf(original, original.length);
System.out.println(Arrays.toString(copy)); // Output: [1, 2, 3]

length:

The length property is used to find the size of an array.

Example
int[] arr = {10, 20, 30, 40, 50};
System.out.println("Length of the array: " + arr.length); // Output: 5

Try it yourself


Converting Array to String:

Converts an array to a string.

Example
import java.util.Arrays;

int[] arr = {10, 20, 30};
System.out.println(Arrays.toString(arr)); // Output: [10, 20, 30]

Try it yourself


Searching in an Array:

Searches for a key in a sorted array.

Example
import java.util.Arrays;

int[] arr = {10, 20, 30, 40};
int index = Arrays.binarySearch(arr, 30);
System.out.println(index); // Output: 2

Try it yourself


Comparing Arrays:

  • Arrays.equals() → Compares two arrays for equality.
  • Arrays.deepEquals() → Compares two multi-dimensional arrays for equality.
Arrays.equals()
import java.util.Arrays;

int[] arr1 = {10, 20, 30};
int[] arr2 = {10, 20, 30};
System.out.println(Arrays.equals(arr1, arr2)); // Output: true

Try it yourself

Arrays.deepEquals()
import java.util.Arrays;

int[][] arr1 = {{1, 2}, {3, 4}};
int[][] arr2 = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepEquals(arr1, arr2)); // Output: true

Try it yourself


Filling an Array:

Fills an array with a specific value.

Example
import java.util.Arrays;

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

Try it yourself


Sorting an Array:

Sorts an array in ascending order.

Example
import java.util.Arrays;

int[] arr = {30, 10, 20, 50};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // Output: [10, 20, 30, 50]

Try it yourself


Copying Arrays:

Copies an array to a new array.

Example
import java.util.Arrays;

int[] original = {1, 2, 3};
int[] copy = Arrays.copyOf(original, original.length);
System.out.println(Arrays.toString(copy)); // Output: [1, 2, 3]

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.