Arrays in java
Arrays
• It is a collection of objects (items) of same type.
• Arrays are objects in java that store multiple object references or
primitives of same type.
• It is a data structure where we store similar elements. We can store
only a fixed set of elements in a Java array.
• Array in Java is index-based, the first element of the array is stored at
the 0th index, 2nd element is stored on 1st index and so on.
Here inside storage locations, here you can directly put the
primitive data type like integer, or you can put references
which is actually referring to some objects.
• Advantages
• It makes the code optimized, we can retrieve or sort the data efficiently (Code
Optimization).
• We can get any data located at an index position (Random access).
• Disadvantages
• We can store only the fixed size of elements in the array. It doesn't grow its size at runtime
(Size Limit).
• To solve this problem, collection framework is used in Java which grows automatically.
• Types of Array in java
• Single Dimensional Array
• Multidimensional Array
Single (One) Dimensional Arrays
• A one-dimensional array is, essentially, a list of like-typed variables.
• To create an array, you first must create an array variable of the
desired type.
• Syntax to Declare an Array in Java
1. dataType[] arr; (or)
2. dataType []arr; (or)
3. dataType arr[];
• For example, the following declares an array named month_days with
the type “array of int”: -- int month_days[];
• Although this declaration establishes the fact that month_days is an array
variable, no array actually exists.
• In fact, the value of month_days is set to null, which represents an array
with no value.
• To link month_days with an actual, physical array of integers, you must
allocate one using new and assign it to month_days.
• new is a special operator that allocates memory.
• month_days = new int[12];
• After this statement executes, month_days will refer to an array of 12 integers.
Further, all elements in the array will be initialized to zero.
• Once you have allocated an array, you can access a specific element
in the array by specifying its index within square brackets.
• All array indexes start at zero. For example, this statement assigns the
value 28 to the second element of month_days.
• month_days[1] = 28;
• The next line displays the value stored at index 3.
[Link](month_days[3]);
• // Demonstrate a one-dimensional array.
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31; When you run this program, it prints the number of days in April.
month_days[1] = 28;
month_days[2] = 31; Java array indexes start with zero, so the number of days in April is month_days[3] or 30.
month_days[3] = 30;
month_days[4] = 31; It is possible to combine the declaration of the array variable with the allocation of the
month_days[5] = 30; array itself, as shown here:
int month_days[] = new int[12];
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
[Link]("April has " + month_days[3] + " days."); } }
• // An improved version of the previous program.
class AutoArray {
public static void main(String args[])
{
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
[Link]("April has " + month_days[3] + " days.");
}
}
1. //Java Program to illustrate how to declare, instantiate, initialize
2. //and traverse the Java array.
3. class Testarray{
4. public static void main(String args[]){
5. int a[]=new int[5];//declaration and instantiation
6. a[0]=10;//initialization
7. a[1]=20;
8. a[2]=70;
9. a[3]=40;
10. a[4]=50;
11. // traversing array
12. for(int i=0;i<[Link];i++)//length is the property of array
13. [Link](a[i]);
14. }}
Declaration, Instantiation and Initialization of Java
Array
• We can declare, instantiate and initialize the java array together by:
int arr []={1,3,2,5};//declaration, instantiation and initialization
1. class ArrayTest{
2. public static void main(String args[]){
3. int arr[]={1,3,2,5};//declaration, instantiation and initialization
4. //printing array
5. for(int i=0;i<[Link];i++)//length is the property of array
6. [Link](arr[i]);
7. }}
Passing Array to a Method in Java
1. //Java Program to demonstrate the way of passing an array
2. //to method.
3. class Test{
4. //creating a method which receives an array as a parameter
5. static void changeA(int [] arr){
6. a[2]=15;
7. }
8. public static void main(String args[]){
9. int arr[]={12,2,16,23};//declaring and initializing an array
10. changeA(arr);//passing array to method
11. }
12. }
Returning Array from the Method
1. //Java Program to return an array from the method
2. class TestReturnArray{
3. //creating method which returns an array
4. static int[] get(){
5. return new int[]{10,30,50,90,60};
6. }
7.
8. public static void main(String args[]){
9. //calling method which returns an array
10. int arr[]=get();
11. //printing the values of an array
12. for(int i=0;i<[Link];i++)
13. [Link](arr[i]);
14. }}
class ArrayExample{
public static void main(String args[]){
int arr[];
arr[0]=12;
arr[1]=13;
}
}
Multidimensional Array
• Syntax to declare MD arrays
• dataType[][] arrayRefVar; (or)
• dataType [][]arrayRefVar; (or)
• dataType arrayRefVar[][]; (or)
• dataType []arrayRefVar[];
• Example to instantiate MD Array in Java
• int[][] arr=new int[4][4];//4 row and 4 column
• Example to initialize MD Array in Java
• arr[0][0]=1;
• arr[0][1]=2;
• arr[0][2]=3;
• arr[1][0]=4;
• arr[1][1]=5;
• arr[1][2]=6;
• arr[2][0]=7;
• arr[2][1]=8;
• arr[2][2]=9;
• arr[3][0]=10;
• arr[3][1]=11;
• arr[3][2]=12;
//Java Program to illustrate the use of multidimensional array
class Testarray {
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{5,1,7},{6,7,5},{3,4,9}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](arr[i][j]+" ");
}
[Link]();
}
}}
class ArrayExample{
public static void main(String args[]){
int arr[][] = new int [4][];
[Link]("arr[0][0] "+ arr[0][0]);
}
}
class ArrayExample{
public static void main(String args[]){
int arr[][] = new int [4][];
[Link]("arr[0][0] "+ arr[0]);
}
}
class ArrayExample{
public static void main(String args[]){
int arr[][] = new int [4][];
[Link](“Length = "+ [Link]);
}
}
class ArrayExample{
public static void main(String args[]){
int arr[][] = new int [4][];
[Link](“Length = "+ arr[0].length);
}
}
class TestArray{
public static void main(String[] args){
//declaring a 2D array with odd columns
int arr[][] = new int[3][];
arr[0] = new int[4];
arr[1] = new int[5];
arr[2] = new int[3];
//initializing an array
int count = 0;
for (int i=0; i<[Link]; i++)
for(int j=0; j<arr[i].length; j++)
arr[i][j] = count++;
//printing the data of an array
for (int i=0; i<[Link]; i++){
for (int j=0; j<arr[i].length; j++){
[Link](arr[i][j]+" ");
}
[Link](); //new line
} }