Array in Java:
Normally, array is a collection of similar type of elements that have contagious memory location. In java, array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed number elements in an array.
Array is index based object, first element of the array is stored at 0 index.
Advantage of using Array:
Randomly access: We can get any data located at any index position.
Code Optimization: It makes the code optimized because we can retrive data easily on index basis and sorting is also easy.
Disadvantage of using Array:
Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime.
To solve this problem, collection framework comes in java.
Types of Array:
There are two types of array
>1.Single Dimensional Array
>2.Multidimensional Array
Single Dimensional Array:
Syntax to Declare an Array in java
dataType[] avariableName; (or)
dataType []avariableName; (or)
dataType avariableName[]
Note: You can declair array in any one of the above 3 ways.
For Example:
int[] age;
String []name;
float price[];
Instantiation of an Array in java:
avariableName=new datatype[size];
For Example:
int[] age=new int[10];
Here age is array of type int of size 10.
Example of single dimensional java array:
simple example of java array, where we are going to declare, instantiate, initialize and traverse an array.
class Test{
public static void main(String []args){
int age[]=new int[6]; //declaration and instantiation of array
age[0]=15; //initialization
age[1]=25;
age[2]=30;
age[3]=40;
age[4]=20;
age[5]=10;
for(int i=0;i<age.length;i++) // here length is the property of array
System.out.println(age[i]);
}
}
Output: 15
25
30
40
20
10
Note: Indexing of array start from 0 to (size-1). i.e. 0 to 5 (6-1)
Declaration, Instantiation and Initialization of Java Array all together:
int age[]={12,20,10,25}; //declaration, instantiation and initialization
For Example:
class Test{
public static void main(String []args){
int age[]={12,20,10,25}; //declaration, instantiation and initialization
for(int i=0;i<age.length;i++)
System.out.println(age[i]);
}
}
Ouput: 12
20
10
25
Multidimensional array:
In such case, data is stored in row and column based index (also known as matrix form).
Syntax to Declare Multidimensional Array in java;
dataType[][] variableName; (or)
dataType [][]variableName; (or)
dataType variableName[][]; (or)
dataType []variableName[];
Note: All the above are the right syntext to declair multidimensional array.
Example to initantiate Multidimensional Array in java:
int[][] age=new int[4][3]; // 4 row and 5 column
Example to initialize Multidimensional Array in java:
age[0][0]=1;
age[0][1]=2;
age[0][2]=3;
age[1][0]=4;
age[1][1]=5;
age[1][2]=6;
age[2][0]=7;
age[2][1]=8;
arr[2][2]=9;
age[3][0]=10;
age[3][1]=11;
arr[3][2]=12;
Example of Multidimensional java array:
class Test{
public static void main(String []args){
//declaring and initializing 2D array
int age[][]={ {1,2,3},{4,5,6},{7,8,9},{10,11,12} };
for(int x=0;i<4;x++){
for(int y=0;j<3;y++){
System.out.print(age[x][y]+" ");
}
System.out.println();
}
}
}
Output: 1 2 3
4 5 6
7 8 9
10 11 12
Passing Java Array in the method:
We can pass the array in the method so that we can reuse the same logic on any array.
simple example to get maximum number of an array using method.
class Test{
static void max(int []arr){
int max=arr[0];
for(int i=1;i<arr.length;i++)
{
if(max<arr[i])
max=arr[i];
}
System.out.println(max);
}
public static void main(String []args){
int age[]={1,7,2,4};
max(age); //passing array in the method
}
}
Output: 7
Normally, array is a collection of similar type of elements that have contagious memory location. In java, array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed number elements in an array.
Array is index based object, first element of the array is stored at 0 index.
Advantage of using Array:
Randomly access: We can get any data located at any index position.
Code Optimization: It makes the code optimized because we can retrive data easily on index basis and sorting is also easy.
Disadvantage of using Array:
Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime.
To solve this problem, collection framework comes in java.
Types of Array:
There are two types of array
>1.Single Dimensional Array
>2.Multidimensional Array
Single Dimensional Array:
Syntax to Declare an Array in java
dataType[] avariableName; (or)
dataType []avariableName; (or)
dataType avariableName[]
Note: You can declair array in any one of the above 3 ways.
For Example:
int[] age;
String []name;
float price[];
Instantiation of an Array in java:
avariableName=new datatype[size];
For Example:
int[] age=new int[10];
Here age is array of type int of size 10.
Example of single dimensional java array:
simple example of java array, where we are going to declare, instantiate, initialize and traverse an array.
class Test{
public static void main(String []args){
int age[]=new int[6]; //declaration and instantiation of array
age[0]=15; //initialization
age[1]=25;
age[2]=30;
age[3]=40;
age[4]=20;
age[5]=10;
for(int i=0;i<age.length;i++) // here length is the property of array
System.out.println(age[i]);
}
}
Output: 15
25
30
40
20
10
Note: Indexing of array start from 0 to (size-1). i.e. 0 to 5 (6-1)
Declaration, Instantiation and Initialization of Java Array all together:
int age[]={12,20,10,25}; //declaration, instantiation and initialization
For Example:
class Test{
public static void main(String []args){
int age[]={12,20,10,25}; //declaration, instantiation and initialization
for(int i=0;i<age.length;i++)
System.out.println(age[i]);
}
}
Ouput: 12
20
10
25
Multidimensional array:
In such case, data is stored in row and column based index (also known as matrix form).
Syntax to Declare Multidimensional Array in java;
dataType[][] variableName; (or)
dataType [][]variableName; (or)
dataType variableName[][]; (or)
dataType []variableName[];
Note: All the above are the right syntext to declair multidimensional array.
Example to initantiate Multidimensional Array in java:
int[][] age=new int[4][3]; // 4 row and 5 column
Example to initialize Multidimensional Array in java:
age[0][0]=1;
age[0][1]=2;
age[0][2]=3;
age[1][0]=4;
age[1][1]=5;
age[1][2]=6;
age[2][0]=7;
age[2][1]=8;
arr[2][2]=9;
age[3][0]=10;
age[3][1]=11;
arr[3][2]=12;
Example of Multidimensional java array:
class Test{
public static void main(String []args){
//declaring and initializing 2D array
int age[][]={ {1,2,3},{4,5,6},{7,8,9},{10,11,12} };
for(int x=0;i<4;x++){
for(int y=0;j<3;y++){
System.out.print(age[x][y]+" ");
}
System.out.println();
}
}
}
Output: 1 2 3
4 5 6
7 8 9
10 11 12
Passing Java Array in the method:
We can pass the array in the method so that we can reuse the same logic on any array.
simple example to get maximum number of an array using method.
class Test{
static void max(int []arr){
int max=arr[0];
for(int i=1;i<arr.length;i++)
{
if(max<arr[i])
max=arr[i];
}
System.out.println(max);
}
public static void main(String []args){
int age[]={1,7,2,4};
max(age); //passing array in the method
}
}
Output: 7
No comments:
Post a Comment