Java - Arrays | W3schools | Tutorials point | W3school

Java – Arrays
Java programming language offers the array, a data structure, which helps in storing a fixed-size collection of elements in sequence of the same type.  Array is helpful in storing a collection of data, however more importantly useful to think array as a variable of same category or type.Java – Arrays

In place of declaring individual variable, like number1, number0…., and number 99 to show individual series.
This tutorial will show how to create arrays, declare array variables, as well as the process how array make use of indexed variables.

Declaring array variables:

To make use of an array in a program,  first it is important to declare a variable in order to reference the array and you have to specify the kind of array it can reference.
dataType[] arrayRefVar;   // preferred way.
dataType arrayRefVar[];  // works but not preferred way.

Note − The style dataType[] arrayRefVar is preferred. The style dataType arrayRefVar[] comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.

The following code snippets are examples of this syntax −

double[] myList;   // preferred way.
double myList[];   // works but not preferred way.

This will gives the result as:



Passing Arrays to Methods:
As same as you pass primitive kind of values to methods, likewise you can pass arrays to methods.  For an example, this method shows the main elements in an int array-


public static void printArray(int[] array) {
  for (int i = 0; i < array.length; i++) {
     System.out.print(array[i] + ” “);

User can supplicate it by passing an array. Like, the below statement supplicate the printArray procedure to show 3,1,2,4 and 2-


printArray(new int[]{3, 1, 2, 6, 4, 2});

Returning an Array:

It is generally the reversal of another array-


public static int[] reverse(int[] list) {
  int[] result = new int[list.length];

  for (int i = 0, j = result.length 1; i < list.length; i++, j–) {
     result[j] = list[i];
  return result;

The Arrays Class:

java.util.Arrays class includes so many static methods for searching and sorting arrays, filing and compare array elements. All these methods are overburden for all types of primitive.

Method & Description

  1. public static int binarySearch(Object[] a, Object key)

    It helps in finding the array of object( Int, Byte, double etc.) for the particular value by making use of binary search algorithm. It is important to classify it in this manner to make this all.This returns index of the search key, if it is contained in the list; otherwise, it returns ( – (insertion point + 1)).

  2. public static boolean equals(long[] a, long[] a2)

    Returns true if any two particular arrays of longs are same and equal to one another. If both the arrays contains the same number of elements than it is considered equal.Two arrays are considered equal. This returns true if the two arrays are equal. Same method could be used by all other primitive data types (Byte, short, Int, etc.)

  3. public static void fill(int[] a, int val)

    Assigns the specified int value to each and every element of the specified array of ints. The same procedure could be easily used by all other primitive data types (Byte, Int, short, etc.)

  4. public static void sort(Object[] a)

    Classify the specified array of objects into ascending order, as per the natural sequence of its elemnts. The similar  method could be used by the rest  primitive data types ( Short, Byte,  Int, etc.)