JAVA

All Languages C CPP JAVA HTML CSS JAVASCRIPT PYTHON

Arrays In Java

An array is a group of like-typed variables that are referred to by a common name.Arrays in Java work differently than they do in C/C++.

Following are some important point about Java arrays.

Array can contains primitives data types as well as objects of a class depending on the definition of array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are stored in heap segment.

Creating, Initializing, and Accessing an Array

One-Dimensional Arrays :

The general form of a one-dimensional array declaration is

type var-name[];
OR
type[] var-name;

An array declaration has two components: the type and the name. typedeclares the element type of the array. The element type determines the data type of each element that comprises the array. Like array of int type, we can also create an array of other primitive data types like char, float, double..etc or user defined data type(objects of a class).Thus, the element type for the array determines what type of data the array will hold.

Example:
// both are valid declarations
intintArray[]; 
or int[] intArray; 

byte byteArray[];
short shortsArray[];
booleanbooleanArray[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];

// an array of references to objects of
// the class MyClass (a class created by
// user)
MyClassmyClassArray[]; 

Object[]  ao,        // array of Object
Collection[] ca;  // array of Collection
                     // of unknown type

Although the above first declaration establishes the fact that intArray is an array variable, no array actually exists. It simply tells to the compiler that this(intArray) variable will hold an array of the integer type. To link intArray with an actual, physical array of integers, you must allocate one using new and assign it to intArray.

Instantiating an Array in Java

When an array is declared, only a reference of array is created. To actually create or give memory to array, you create an array like this:The general form of new as it applies to one-dimensional arrays appears as follows:

var-name = new type [size];

Here, type specifies the type of data being allocated, size specifies the number of elements in the array, and var-name is the name of array variable that is linked to the array. That is, to use new to allocate an array, you must specify the type and number of elements to allocate.

Example:
intintArray[];    //declaring array
intArray = new int[20];  // allocating memory to array
OR
int[] intArray = new int[20]; // combining both statements in one
Note :

1. The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types).Refer Default array values in Java

2. Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. Thus, in Java all arrays are dynamically allocated.

Array Literal

In a situation, where the size of the array and variables of array are already known, array literals can be used.

int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; 
 // Declaring array literal

• The length of this array determines the length of the created array.

• There is no need to write the new int[] part in the latest versions of Java Accessing Java Array Elements using for Loop Each element in the array is accessed via its index. The index begins with 0 and ends at (total array size)-1. All the elements of array can be accessed using Java for Loop.

// accessing the elements of the specified array for (inti = 0; i < arr.length; i++) System.out.println("Element at index " + i + " : "+ arr[i]);
Implementation:
// Java program to illustrate creating an array 
// of integers,  puts some values in the array, 
// and prints each value to standard output. 

  

classGFG  
{ 

    publicstaticvoidmain (String[] args)  

    {          

      // declares an Array of integers. 

      int[] arr; 

          

      // allocating memory for 5 integers. 

      arr = newint[5]; 

          

      // initialize the first elements of the array 

      arr[0] = 10; 

          

      // initialize the second elements of the array 

      arr[1] = 20; 
      //so on... 

      arr[2] = 30; 

      arr[3] = 40; 

      arr[4] = 50; 
      // accessing the elements of the specified array 

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

         System.out.println("Element at index "+ i +  

                                      " : "+ arr[i]);           

    } 
} 

Output:
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50