We can initialize arrays at time of declaration of array.
Syntax:-
datatype variable_name [] = {value1, value2.... valueN};
Example:-
int sum[] = {2, 8, 12, 895, 147};
datatype specifies the datatype of each element present in array. Datatype of each element should be same. Here datatype may be byte, int, long, float, double and char.
variable_name is name of variable.
List of value/data is separated by comma between curly braces. The commas separated the values of the array element.
There is no need to use new operator because the array automatically create size and memory required for element.
Example:-
class group{
public static void main(String arng[]){
int sum[]={5,68,78,84};
System.out.println("index \t value" );
for(int i=0; i < sum.length; i++)
System.out.println(i+ "\t"+sum[i]);
}
}
Output:-
index value
0 5
1 68
2 78
3 84
Previous Code:-
Arrays
List Code:-
Java codes
Next Code:-
Length function for array
Java Programs:-
List of Java Programs
Syntax:-
datatype variable_name [] = {value1, value2.... valueN};
Example:-
int sum[] = {2, 8, 12, 895, 147};
datatype specifies the datatype of each element present in array. Datatype of each element should be same. Here datatype may be byte, int, long, float, double and char.
variable_name is name of variable.
List of value/data is separated by comma between curly braces. The commas separated the values of the array element.
There is no need to use new operator because the array automatically create size and memory required for element.
Example:-
class group{
public static void main(String arng[]){
int sum[]={5,68,78,84};
System.out.println("index \t value" );
for(int i=0; i < sum.length; i++)
System.out.println(i+ "\t"+sum[i]);
}
}
Output:-
index value
0 5
1 68
2 78
3 84
Previous Code:-
Arrays
List Code:-
Java codes
Next Code:-
Length function for array
Java Programs:-
List of Java Programs
Leave reply
Add your comments here