Topic outline



    • An array is a collection of variables of similar types that are referred to by a common name. In Java, arrays function differently than they do in C / C++. Some important points about Java arrays are listed below. 


      1. All arrays in Java are dynamically assigned.(Discussed below) 

      2. Since arrays are objects in Java, we can use the object property length to find their length. This is different from C / C++, where we use size to find length.

      3. After the data sort, a Java array variable may also be declared like other variables with]. 

      4. The variables in the array are ordered and each of them has an index starting at 0. 

      5. It is also possible to use the Java array as a static field, as a local variable or as a function parameter. 

      6. An int value must define the size of an array and not long or short. 

      7. An Object is the direct superclass of an array sort.

      8. Each type of array implements the Cloneable and Java.io. Serializable interfaces. 



      Depending on the array description, the array may contain primitives (int, char, etc.) as well as object (or non-primitive) class references. For primitive data types, the actual values are stored at contiguous locations in the memory. In the case of class objects, the individual objects in the heap section are stored.


      There are two components to an array declaration: the type and the name. Type declares the array's element type. The type of element specifies the type of data for each element comprising the array. We may also construct an array of other primitive data types, such as char, float, double, etc., or user-defined data types (class objects), including an array of integers. The element type for the array therefore defines what type of data the array is going to carry.


      Example : 


      package examples;

      public class arrays_example {

      public static void main(String[] args)

      {

      int[] sample_array_integer = {10,20,30,40,50,60};

      String[] sample_array_string = {"Animals", "Birds", "Humans"};

      //length of arrays

      System.out.println("Integer array length = " + sample_array_integer.length);

      System.out.println("String array length = " + sample_array_string.length);

      //accessing the elements of arrays using indexing or loops

      //indexing starts form 0 as the first element

      System.out.println(sample_array_string[2]);

      System.out.println(sample_array_integer[5]);

      //printing all elements in an array using for loop

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

      {

      System.out.println(sample_array_string[i]);

      }

      }

      }


      Instantiation in Java of an Array 


      When an array is declared, it just produces an array reference. You create an array like this to actually create or give memory to the array: The general new form as it relates to one-dimensional arrays appears as follows: 


      Var-name = new [type] size;


      Using Loop to access Java Array Elements 


      In the list, each element is accessed via its index. The index starts with 0 and finishes at (total size of the array)-1. You can access all the array elements using the Java for Loop feature.


      // accessing the elements of the specified array

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

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

                                      " : "+ arr[i]);