Java Array

arrays in java

A Java Array is a collection of variables of the same type.

When you need to store a list of values, such as numbers, you can store them in an array, instead of declaring separate variables for each number.

To declare an array, you need to define the type of the elements with square brackets.


Declaring Array Variables:

To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable :

Example:

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

Creating Arrays:

The array elements are accessed through the index. Array indices are 0-based; that is, they start from 0 to arrayRefVar.length-1.

Example:

Following statement declares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList :

double[] myList = new double[10];

The foreach Loops:

JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.

Example:

public class TestArray {
   public static void main(String[] args) {
      double[] myList = {1.9, 2.9, 3.4, 3.5};
      // Print all the array elements
      for (double element: myList) {
         System.out.println(element);
      }
   }

Multidimensional Arrays:

Multidimensional arrays are array that contain other arrays. The two-dimensional array is the most basic multidimensional array.

To create multidimensional arrays, place each array within its own set of square brackets.

Example:

                    int[ ][ ] sample = { {1, 2, 3}, {4, 5, 6} };
                    int x = sample[1][0];
System.out.println(x);
// Outputs 4

The Java Array Class:

The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements. These methods are overloaded for all primitive types.

Sr.No.   Method     Description
1.public static int binarySearch(Object[] a, Object key)Searches the specified array of Object ( Byte, Int , double, etc.) for the specified value using the binary search algorithm. The array must be sorted prior to making this call. 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 the two specified arrays of longs are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are 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 element of the specified array of ints. The same method could be used by all other primitive data types (Byte, short, Int, etc.)
4.public static void sort(Object[] a)Sorts the specified array of objects into an ascending order, according to the natural ordering of its elements. The same method could be used by all other primitive data types ( Byte, short, Int, etc.)

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *