Array in Java

What is an Array?
An array is storehouse or we can say collection of similar type of data type.
It is a data structure where we store similar elements. We can store only fixed set of elements in a java array(solution is ArrayList in Java) unlike vbscript where we can have dynamic arrays using Redim keyword.

Stop being in LOVE with someone who never was and focus on the one who IS. If you Love learning new concepts,Do follow us on FB , G+ , Twitter, or Linkedin

Why Arrays?
It provides code optimization and easy access. Like Instead of declaring individual variables, such as Learn0, Learn1, …Learn9, we can declare one array variable and use Learn[0], Learn[1], …, Learn[9] to represent individual variables.

Syntax:-
dataType[] arrayName;

or

dataType arrayName[];

or

dataType []arrayName;

Memory allocation of Java Array
Memory allocation of an Array

Example:- 
We are declaring an integer array by implementing the above syntax

public class ArrayInJava {
//Array Delcaration in Java
public static void main(String[] args) {
//Approach1
int[] Learn;
//Approach2
int Revise[];
//Approach3
int []implement;
}

}


Assigning a Value :-
We have declared an array, now how can add value into the reserved memory locations of an array like Learn[0] should have 100.

Heap Memory after Array value allocation in Java


Approach1:-
dataType[] arrayName = new datatype[arraysize];
arrayName[0] = arrayValue;
arrayName[1] = arrayValue;



arrayName[arraySize-1] = arrayvalue;

Approach2:- 
dataType[] arrayName = {value1,value2,…value10}

Example:-

public class ArrayInJava {
//Assigning values to Array in Java
public static void main(String[] args) {
//Approach1
int[] Learn = new int[2];
Learn[0] = 100 ;
Learn[1] = 200 ;
//Approach2
int Revise[] = {100,200};
//Accessing element of an Array
System.out.println("Approach one Result ="+Learn[0]+";"+Learn[1]);
System.out.println("Approach Two Result ="+Revise[0]+";"+Revise[1]);
}

}
#Output:-
Approach one Result =100;200
Approach Two Result =100;200

Note:-
Array index always starts with 0 , thus to access the first element we used ArrayName[0].

HandOn:-
Example1:- 
We have used for loops in working with Arrays.

public class ArrayInJava {
//Working with Arrays Using For Loops
public static void main(String[] args) {
//Declaring an Array
int[] Learn = new int[10];
//Assinging values to an Array Using For loop
for (int iCnt = 0 ;iCnt<Learn.length;iCnt++){
Learn[iCnt]=100+iCnt;
//Taking the output
System.out.println(Learn[iCnt]);
}
}
}

#Output:-
100
101
102
103
104
105
106
107
108
109

Example2:- Passing an  Array to a method and Taking return type also as an Array(This is also an example how we can receive multiple return values from an function)

public class ArrayInJava {
//Passing and Reciveing an Array from a method
public static void main(String[] args) {
//Declaring an Array
int[] Learn = new int[10];
//Assinging values to an Array Using For loop
for (int iCnt = 0 ;iCnt<Learn.length;iCnt++){
Learn[iCnt]=100+iCnt;
}

//Calling a method to add 1 to all the array values
int[] FinalRes = returnArray(Learn);
//Taking the output of an array
for (int iCnt = 0 ;iCnt<FinalRes.length;iCnt++){
System.out.println(FinalRes[iCnt]);
}
}

public static int[] returnArray(int[] res){
//Adding 1 to each value of an Array
for (int iCnt = 0 ;iCnt<res.length;iCnt++){
res[iCnt]= res[iCnt]+1;
}
//Returning an Array Value
return res;
}
}

#Output:-
101
102
103
104
105
106
107
108
109
110


Note:- We can use Array.sort(ArrayName); to sort values in a Array.

Multidimensional Arrays:-
Data is stored in the form of rows and columns like a matrix or they way we access the elements of an excel cell A1, B1 ….

Syntax:-
dataType[][] arrayName;

Example:-
int[][] Learn=new int[4][4];//4 row and 4 column

Q&A:-
1.What is the difference between char c[] and char[] c in array declaration ?
A. In Java, both are legal and equivalent.First style is preferred declaration and second is  how arrays were declared in C , so it was included for the convenience of programmer moving from c/c++ to Java

2. What is the starting index of an Array in JAVA/vbscript and Why?
A. Starting index is always “0”.This convention originated with machine-language programming, where the address of an array element would be computed by adding the index to the address of the beginning of an array. Starting indices at 1 would entail either a waste of space at the beginning of the array or a waste of time to subtract the 1.

3. What happens if we use a negative number(index) to access an array?
A. Whenever we attempt to index an array with an index that is not between zero and the array length minus one, Java will issue an ArrayIndexOutOfBoundsException and terminate the program.


ufthelp

Leave a Reply

Your email address will not be published. Required fields are marked *