# ARRAYS IN JAVA
1. Java array is an object which contains elements of a similar data type.
2. The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements.
3. We can store only a fixed set of elements in a Java array.
4. The length of an array is established when the array is created.
5. Each item in an array is called an element, and each element is accessed by its numerical index
## Creating, initializing, instantiation, and accessing an array :
1. Syntax to declare an array in Java :
dataType arrName[size];
2. Syntax to initialize an array in Java :
int a[ ] = {33, 3, 4, 5};
3. Instantiation of an array in Java :
arrayRefVar = new datatype[size];
4. Accessing an array element : Each array element is accessed by its
numerical index.
System.out.println(“Element 1 at index 0 :” + anArray[0]);
System.out.println(“Element 2 at index 1 :” + anArray[1]);
System.out.println(“Element 3 at index 2 :” + anArray[2]);
## Types of Array in java:
There are two types of arrays.
1. Single-Dimensional Array

2. Multidimensional Array

## Examples:
Example of Java Array
//Java Program to illustrate how to declare, instantiate, initialize
//and traverse the Java array.
class Testarray{
public static void main(String args[]){
int a[]=new int[5]; //declaration and instantiation
a[0]=10; //initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++) //length is the property of array
System.out.println(a[i]);
}}
Output:
10
20
70
40
50
## Create an Array of Objects in Java:

Syntax:
ClassName obj[]=new ClassName[array_length];
Or
ClassName[] objArray;
Or
ClassName objeArray[];
#### Example:
// Java program to illustrate creating
// an array of objects
class Student {
public int roll_no;
public String name;
Student(int roll_no, String name)
{
this.roll_no = roll_no;
this.name = name;
}
}
// Elements of the array are objects of a class Student.
public class GFG {
public static void main(String[] args)
{
// declares an Array of integers.
Student[] arr;
// allocating memory for 4 objects of type Student.
arr = new Student[4];
// initialize the first elements of the array
arr[0] = new Student(1, "priyansh");
// initialize the second element of the array
arr[1] = new Student(2, "Vikas");
// so on...
arr[2] = new Student(3, "shikar");
arr[3] = new Student(4, "Dharmesh");
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at " + i + " : "
+ arr[i].roll_no + " "
+ arr[i].name);
}
}
Output
Element at0: 1 priyansh
Element at 1 : 2 Vikas
Element at 2 : 3 shikar
Element at3 : 4 Dharmesh
Time Complexity: O(n)
Auxiliary Space: O(1)
### ArrayIndexOutOfBoundsException:
It is thrown to show that the array has been accessed with the wrong use of an index.
Example:
public class GFG {
public static void main(String[] args)
{
int[] arr = new int[3];
arr[0] = 20;
arr[1] = 40;
arr[2] = 60;
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
}
Output
20
40
60
Arrays Types and Their Allowed Element Types
Primitive Type Arrays: Any type which can be implicitly promoted to declared type.
Object Type Arrays: Either declared type objects or their child class objects.
Abstract Class Type Arrays: Its child-class objects are allowed.
Interface Type Arrays: Its implementation class objects are allowed.
### Conclusion:
Arrays are a important topic in java. If we are an expert in arrays and string in every programming language, then it will be easy to learn .