Array is collection of similar kind of data. Array in java is index based , first element of the array is stored at 0 index.
Benefits (Advantages) of array:-
- Code Optimization
- Performance increase
Disadvantage Disadvantage of array is "size limit ".
Types of array:
- Single Dimensional Array
- Multidimensional Array
Many types of syntax to declared in array:
- dataType[] arr; (or)
- dataType []arr; (or)
- dataType arr[];
1 Example of single Dimensional array
/**
*
* @author pradeep
*/
class ArrayDemo {
public static void main(String[] args) {
/*int marks[]=new int[5];
marks[0]=33;
marks[1]=65;
marks[2]=69;
marks[3]=78;
marks[4]=86;
* */
//int marks[]=new int[]{33,65,69,78,86};
int marks[]={33,65,69,78,86};
for(int i=0;i<5;i++)
{
System.out.println("print marks:"+marks[i]);
}
}
}
2. Example of MultiDimensional array
/**
*
* @author pradeep
*/
class MultiDimensionalArray{
public static void main(String args[]){
//declaring and initializing 2D array
int array[][]={{6,7,8},{4,6,8},{3,6,9}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print("Multi Dimensional Array" +array[i][j]+" ");
}
System.out.println();
}
}}
Result: Output of this result is:
run:
Multi Dimensional Array6 Multi Dimensional Array7 Multi Dimensional Array8
Multi Dimensional Array4 Multi Dimensional Array6 Multi Dimensional Array8
Multi Dimensional Array3 Multi Dimensional Array6 Multi Dimensional Array9
BUILD SUCCESSFUL (total time: 0 seconds)
Note: If you have any problem then reply me.
No comments:
Post a Comment