Arrays
What is an Array?
- An array is a data structure consisting of collection of elements(values or variables), where each element can be identified by a unique key.
- Where unique key means elements of each value is identified by atleast one array index.
- From the above example, the array size is 6 because array index starts from 0. For each array element there exists minimum of one array index. From the above example, if you want the array element “10” then you need to call array index “2”.
Why we use Arrays in Java?
- Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
- To reduce code complexity.
- For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names.
String[] array = new String[100];
or
String array[] = new String[100];
Here, the above array cannot store more than 100 names. The number of values in a Java array is always fixed. Here the maximum array index is “99” because arrays index stats from “0” i.e, 0,1,2,3,…….,98,99.
Advantages of Array:
- Easier access to any element using the index.
- Easy to manipulate and store large data.
Disadvantages of Array:
- Fixed size. Can not be increased or decrease once declared.
- Arrays can only store values of same data type (Eg: Strings, Integers, Floating numbers etc).
Types of arrays:
There are 3 types of arrays in java. They are:
- One-Dimensional Array(1-d array)
- Two-Dimensional Array(2-d array)
- Multi-Dimensional Array(3-d array)
- One-Dimensional Array:
One-Dimensional array is also known as a linear array, the elements are stored in a single row. For example,
In this example, we have an array of five elements. They are stored in a single line or adjacent memory locations.
2. Two-Dimensional Array:
2D array can be defined as an array of arrays.
The 2D array is organized as matrices which can be represented as the collection of rows and columns.
The syntax for 2D aray is :
int arr[max_rows][max_columns];
int[ ][ ] Array = new int[3][4];
From the above example an array consists of 3 rows and 4columns.
3. Multi-Dimnsional Array:
This is a combination of two or more arrays or nested arrays.
We can even use more than two rows and columns.
Array Exceptions in Java
The main Array Exceptions in Java are:
- NullPointerException
- NegativeArraySizeException
- ArrayIndexOutOfBoundsException
- ArrayStoreException
NullPointerException:
In Java a NullPointerException is a class which also extends RuntimeException. There are the following conditions where the NullPointerException is generated. They are thrown when an application attempts to use null in a case where an object is required.
- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
Object obj = null;
But, you cannot use an object with null value or (a null value instead of an object) if you do so, a NullPointerException will be thrown.
NegativeArraySizeException:
This error is thrown when anyone wants create an array with a negative size.
NegativeArraySizeException is a class in Java which extends RuntimeException.
The simple example of NegativeArraySizeException is
class Main {
public static void main(String[] args) {
int[] Array = new int[-4];//NegativeArraySizeException occurs here because the array size should not be Negative
for (int i = 0; i < Array.length; i++) {
System.out.println(Array[i]);
}
}
}
Output:
Exception in thread "main" java.lang.NegativeArraySizeException
at Main.main(Main.java:4)
ArrayIndexOutOfBoundsException:
This type of error is generated when an array has been accessed with an illegal index. In other words when an array is accessed by a negative index or more than the size of the array. In Java it’s a separate class and this class extends the IndexOutOfBoundException class.
The ArrayIndexOutOfBoundsException is a Runtime Exception thrown only at runtime. The Java Compiler does not check for this error during the compilation of a program.
A simple example for ArrayIndexOutOfBoundsException is shown below:
int[] numbers = new int[] {1, 2, 3, 4, 5};
int lastNumber = numbers[5];
Here, the size of the array is 5, which means the index will range from 0 to 4. In this case, accessing the 5th index results in an Array Index Out Of Bounds Exception.
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at ...
ArrayStoreException :
This type of exception is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException. ArrayStoreException is also a separate class in Java and it extends RuntimeException.
A simple example for ArrayStoreException is show below:
Object array[] = new String[5];
array[0] = 2;
The exception will be thrown at the second line of code when we try to store an incorrect value type in the array.