Arrays in Java are used to store multiple values under single variable name, it is useful when we are dealing with a large set of data. The starting element address is considered as base address, it starts with 0th element.
Array is a data structure.
It stores the group of elements of Homogeneous(same) Data type .
Arrays in java are Dynamically stored.
Elements in array have their indexes starting from 0.
Syntax for Arrays in Java:
Creating and declaring array:
data_type array_name[]; or data_type[] array_name;
Example:
String str_ar[]; or String[] str_ar;
• Both the syntaxes are correct, we can use any one of them, where string is datatype and str_ar is variable name and name of an array.
• But here, we just declared and referenced array and not allocated memory. Without allocating memory, the arrays doesn't exist physically.
Allocating memory or Instantiating an Array
var_name = new data_type [size_array];
Example:
str_ar = new String[5];
• new keyword is used to allocating memory to an array.
• In the above example we took array size as 5 , that is we can store 5 elements of data type string.
We can write both steps in one line
data_type array_name[] = new data_type [size_array];
or data_type[] array_name = new data_type [size_array];
Example:
String str_ar[] = new String[5];
or String[] str_ar = new String[5];
Storing and Accessing the elements in array:
• We access the array elements by using indexes.
• Default value of the elements , if the datatype is
byte ,short & int is 0 ,
long is 0L float is 0.0f,
double is 0.0d
char is '\u0000' i.e 0
string or any object is null
boolean is false
Example:
let us store the data in Array
String[] str_ar[] = new String[]{" iam"," new"," learner"," to"," Programming9"}; or String[] str_ar[] = new String[5]; str_ar[0] ="Iam" str_ar[1] ="new"; str_ar[2] ="learner"; str_ar[3] ="to"; str_ar[4] ="Programming9";
To access we need to use loops;
for (int i = 0; i < str_ar.length; i++) { System.out.println(str_ar[i]); // Iam new learner to Programming9 }
length is a final variable used to find the size of array.
Example for 1D string array:
public class Pro9 { public static void main (String[] args) { // declares and allocate an Array of integers. String str_ar[]= new String[5]; // initialize the elements of the array str_ar[0] ="Iam"; str_ar[1] ="new"; str_ar[2] ="learner"; str_ar[3] ="to"; str_ar[4] ="Programming9"; // accessing the elements for (int i = 0; i < str_ar.length; i++) { System.out.println(str_ar[i]); // Iam new learner to Programming9 } } }
output:
Iam new learner to Programming9
Types of arrays:
The above array is basic one dimensional array , we have 3 types of arrays in Java
• One dimensional array
• Two dimensional array
• Multidimensional array
Two dimensional array : Two dimensional array is just array of arrays i.e elemts are stores in row and coloumns syntax:
data-type[][] array_name =new data_type[row_size][coloumn_size];
• The number of elements in array or the size of arrow is row_size*coloumn_size.
Example:
int[][] int_ar=new int[3][4];
• The above example has 3 rows and 4 coloumns ,so the size of array is 3*4=12
• similar to the 1D array to access we need to use for loops but two
Example:
for (int i = 0; i < int_ar.length; i++) { //int_ar.length gives rows length for (int j= 0; j= int_ar[0].length; j++) { //int_ar[0].length gives coloumn length System.out.println(str_ar[i][j]); } }
Example for 2D array using string:
import java.util.Scanner; public class Pro9 { public static void main(String[] args) { Scanner ob =new Scanner(System.in); System.out.println("Enter number of rows an coloumns"); int r=ob.nextInt(); int c=ob.nextInt(); // declares and allocate an Array of integers. String str_ar[][]= new String[r][c]; // Reading the elements of the array from user System.out.println("Enter the elememts"); for (int i = 0; i < str_ar.length; i++) { for (int j = 0; j < str_ar[0].length; j++) { str_ar[i][j]=ob.next(); //To read string we use next() } } // accessing the elements System.out.println("String Array"); for (int i = 0; i < str_ar.length; i++) { for (int j = 0; j < str_ar[0].length; j++) { System.out.print(str_ar[i][j]+" "); //printing the elements } System.out.println(); } } }
output:
Enter number of rows an coloumns 3 3 Enter the elememts aa bb cc dd ee ff gg hh ii String Array aa bb cc dd ee ff gg hh ii
Multidimensional array:
Multidimensional array is n-dimensional array(n>=2) and it's mostly used in 3D image processing
syntax:
data-type[][].....[] array_name = new data_type[size1][size2].....[sizen];
example:
if n=3 then int int_arr[][][]= new int[3][4][5];
Pro's of arrays:
• Arrays are more efficient way to manage memory and organize data.
• Storing many elements at a time.
• It allows random accessing of elements.
Con's of arrays:
• Predetermining the size of array is must.
• To delete one element we need to traverse through out the way.
• More dimensions more difficult to access the particular element.