Array in Java

Arrays 

  • An array is a group of like-typed variables that are referred to by a common name.
  • Array can contain primitives data types as well as objects of a class.
  • It can store a fixed-size.
  • It can store value in sequential collection of elements.
  • It has the same data type for all elements.
    • Syntax: dataType[] arrayName;
      • dataType can be a primitive data type like int, char, double, byte, etc. or an object.
      • arrayName is an identifier.
  • Example: double[] data;
    • dataType is double
    • arrayName is data.
  • But, how many elements can array this hold?
    • Here, The length of data array is 10. Meaning, it can hold 10 elements.
    • Example: data = new Double[10];
      • Method - 01: Step - 01 Declaration, Step-02 Allocation
      • Example:
int[] age;
age = new int[5];
      • Method - 02: Both in single step
      • Example:
int[] age = new int[5];
  • How to access elements in array?
    • You can access elements of an array by using indices.
    • Example: int[] age = new int[5];
      • The first element of array is age[0], 
      • The second element is age[1] and so on.
      • If the length of an array is n, the last element will be arrayName[n-1]. 
      • Example: Length of age array is 5, the last element of the array is age[4]
    • The default initial value of elements of an array is 0 for numeric types and false for boolean.
  • Full Example:
class ArrayExample {
   public static void main(String[] args) {
   
      int[] age = new int[5];
   
      System.out.println(age[0]);
      System.out.println(age[1]);
      System.out.println(age[2]);
      System.out.println(age[3]);
      System.out.println(age[4]);
   }
}
  • Advantages
    • Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
    • Random access: We can get any data located at an index position.
  • Disadvantages
    • Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. 
Note: To solve this problem, the collection framework is used in Java which grows automatically.
  • Types of Array:
    • An array can be One-Dimensional, Two-Dimensional and Multidimensional.
  • One-Dimensional
    • Syntax: datatype[] array_name=new datatype[SIZE]
    • Example: int[] a=new int[3]
  • Two-Dimensional
    • Syntax: Data_Type[][] Array_Name = new Data_Type[SIZE][SIZE];
    • Example: double [][] anStudentArray = new int[3][3];
  • Multidimensional
    • Syntax: Data_Type[][][] Array_Name = new Data_Type[SIZE][SIZE][SIZE];
    • Example: double [][][] anStudentArray = new int[3][3][3];

How to initialize arrays?
  • One-Dimensional
    • Syntax: array_name[index] = value;
    • Example: a[0]=10;
  • Two-Dimensional
    • Syntax: Array_name[row_index][column_index]=value;
    • Example: anStudentArray[0][0] = 15;
How to multiple initialize arrays?
  • One-Dimensional
    • Syntax: datatype[] array_name = {value1, value2, value3...};
    • Example: int[] data = {10,20,30,40,50,60,71,80,90,91};
  • Two-Dimensional
    • Syntax: datatype[][] array_name ={ {value1,value2...},{value1,value2,...}...};
    • Example: double [][] anStudentArray = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}};
How to display arrays elements?
  • One-Dimensional:
    • Syntax: array_name[index]
    • Example: System.out.println(a[0]);
  • Two-Dimensional:
    • Syntax: array_name[row_index][column_index]
    • Example:System.out.println(anStudentArray [0][2]);
Variable Size Array:
  • Array can two types:
    • 1. Static Array
      • A static array is a fixed-size array.
      • We can add/delete/update elements up to given size.
    • 2. Dynamic Array
      • A dynamic array is a variable-size array.
      • We can add/delete/update elements more than size.
      • A Dynamic array automatically grows when we try to make an insertion and there is no more space left for the new item.
  • Example:
    • Java has built-in dynamic arrays. 
    • These are Vector, ArrayList and LinkedList.
      • ArrayList is a resizable array implementation of the List interface. It implements all optional list operations and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. 
      • The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. The size of a Vector can grow or shrink as needed to accommodate adding or removing items after Vector has been created. Unlike the new collection implementations, Vector is synchronized. if a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.
      • The LinkedList is a doubly-linked list implementation of the List and Deque interfaces. It implements all optional list operations and permits all elements (including null).
  • Key Features of Dynamic Array:
    • Add Element: Add element at the end if the array size is not enough then extend the size of the array and add an element at the end of the original array as well as given index.
    • Delete Element: Delete an element from array, default remove() method delete an element from end, simply store zero at last index and you also delete element at specific index by calling removeAt(i) method where i is index.
    • Resize of Array Size: When the array has null/zero data(exclude add by you) at the right side of the array whose take unrequited memory, the method srinkSize() free extra memory.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form