Here is an example of comparing two arrays in Java: int[] primes = {3, 5, 7}; int[] odds = {3, 5, 7}; boolean isEqual = Arrays.equals(primes, odds); if (isEqual) { System.out.printf("array %s and %s are equal %n", Arrays.toString(primes), Arrays.toString(odds)); } ...
In Java, here is how we can declare an array. dataType[] arrayName; dataType – it can be primitive data types like int, char, double, byte, etc. or Java objects arrayName – it is an identifier For example, double[] data; Here, data is an array that can hold values of type ...
Basic Java Program to Reverse an int Array In this first example, we take the size of array and the elements of array as input. We consider a function reverse which takes the array (here array) and the size of an array as the parameters. Inside the function, we initialize a new array...
There are several ways how we can initialize an array in Java. In the first example, an array is created and initialized in two steps. Main.java import java.util.Arrays; void main() { int[] a = new int[5]; a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; a[4] = 5; ...
Example 2: Print an Array using standard library Arrays import java.util.Arrays; public class Array { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; System.out.println(Arrays.toString(array)); } } Output [1, 2, 3, 4, 5] In the above program, the...
Description:This method sorts the array in ascending order. Example: import java.util.Arrays; public class Main { public static void main(String[] args) { // define the array int[] intArray = {10,4,25,63,21,51,73,24,87,18}; ...
publicclassJavaExample{publicstaticvoidmain(Stringargs[]){intnumber[]={1,5,7,9,11};for(inti=0;i<=number.length;i++){System.out.println(number[i]);}}} Output: Recommended Articles: 1)Java program to print number of elements in an array ...
An example program to add an element using the new array is shown below. importjava.util.*; public class Main { public static void main(String[] args) { //original array String[] colorsArray = {"Red", "Green", "Blue" }; System.out.println("Original Array: " + Arrays.toString(colo...
import java.util.Scanner; //program uses Scanner class public class ArrayElements { public static void main(String[] args) { int[] numArray = new int[5]; int i; Scanner input=new Scanner(System.in); System.out.print("Enter the 5 Array Elements : "); for(i=0; i<5...
Example: Array: [10, 20, 30, 40, 50] Indices: -5 -4 -3 -2 -1 To learn about Queue, go through our Queue in Java Blog. Get 100% Hike! Master Most in Demand Skills Now ! By providing your contact details, you agree to our Terms of Use & Privacy Policy Types of Array in ...