Array is a collection of similar data types in which each element is accessed by its index value and located in separate memory location.
Syntax-
datatype variable_name = new datatype[size];
Example:-
int sum = new int[12];
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.
new is a special operator that allocate memory for array.
size indicates the maximum number of elements that can be store in array.
Here sum is variable name and int is integer datatype.
Note:-
# Index value is start from zero and ends at size-1
# In java all array are dynamically allocated.
# Skip value is set to zero in array
# Boolean datatype is not used in arrays
# If we try to access element outside range of the array than we get run time error.
# index values always positive number
Different types of array declaration
Following syntax also be used to declaration of array
Syntax:-
datatype variable_name [] = new datatype[size];
or
datatype[] variable_name = new datatype[size];
Example
int sum[] = new int[12];
or
int[] sum = new int[12];
Example:-
simple java array program
class group{
public static void main(String arng[]){
int sum[]=new int[5];
sum[0]= 1;
sum[1]= 4;
sum[2]= 27;
sum[3]= 4;
sum[4]= 148;
System.out.println("index \t data");
for(int i=0; i<5;i++)
System.out.println(i+ "\t"+sum[i]);
}
}
Output:-
index data
0 1
1 4
2 27
3 4
4 148
program for sum of array element
import java.util.Scanner;
class group{
public static void main(String arng[]){
int total = 0;
int sum[]=new int[5];
Scanner data = new Scanner(System.in);
System.out.println("Enter 5 element of array");
for(int i=0; i<5;i++)
sum[i] = data.nextInt();
for(int i=0; i < 5;i++)
total = total +sum[i];
System.out.println("Sum of array element is:"+total);
}
}
Skip value is set to zero in array
class group{
public static void main(String arng[]){
int sum[]=new int[5];
sum[0]= 420;
sum[1]= 847;
sum[2]= 479;
sum[4]= 147;
for(int i=0; i < 5; i++)
System.out.println(sum[i]);
}
}
Output:-
index5
420
847
479
0
147
Here index 3 is skipped so value is set to zero
Java program for character array
class group{
public static void main(String arng[]){
char sum[]=new char[6];
sum[0]= 'a';
sum[1]= 'b';
sum[2]= 'o';
sum[3]= 'u';
sum[5]= 't';
for(int i=0; i < 6; i++)
System.out.println(sum[i]);
}
}
Output:-
index6
a
b
o
u
t
Here index 4 value is skipped
Previous Code:-
Return
List Code:-
Java codes
Next Code:-
Initialization of arrays
Java Programs:-
List of Java Programs
Syntax-
datatype variable_name = new datatype[size];
Example:-
int sum = new int[12];
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.
new is a special operator that allocate memory for array.
size indicates the maximum number of elements that can be store in array.
Here sum is variable name and int is integer datatype.
Note:-
# Index value is start from zero and ends at size-1
# In java all array are dynamically allocated.
# Skip value is set to zero in array
# Boolean datatype is not used in arrays
# If we try to access element outside range of the array than we get run time error.
# index values always positive number
Different types of array declaration
Following syntax also be used to declaration of array
Syntax:-
datatype variable_name [] = new datatype[size];
or
datatype[] variable_name = new datatype[size];
Example
int sum[] = new int[12];
or
int[] sum = new int[12];
Example:-
simple java array program
class group{
public static void main(String arng[]){
int sum[]=new int[5];
sum[0]= 1;
sum[1]= 4;
sum[2]= 27;
sum[3]= 4;
sum[4]= 148;
System.out.println("index \t data");
for(int i=0; i<5;i++)
System.out.println(i+ "\t"+sum[i]);
}
}
Output:-
index data
0 1
1 4
2 27
3 4
4 148
program for sum of array element
import java.util.Scanner;
class group{
public static void main(String arng[]){
int total = 0;
int sum[]=new int[5];
Scanner data = new Scanner(System.in);
System.out.println("Enter 5 element of array");
for(int i=0; i<5;i++)
sum[i] = data.nextInt();
for(int i=0; i < 5;i++)
total = total +sum[i];
System.out.println("Sum of array element is:"+total);
}
}
Skip value is set to zero in array
class group{
public static void main(String arng[]){
int sum[]=new int[5];
sum[0]= 420;
sum[1]= 847;
sum[2]= 479;
sum[4]= 147;
for(int i=0; i < 5; i++)
System.out.println(sum[i]);
}
}
Output:-
index5
420
847
479
0
147
Here index 3 is skipped so value is set to zero
Java program for character array
class group{
public static void main(String arng[]){
char sum[]=new char[6];
sum[0]= 'a';
sum[1]= 'b';
sum[2]= 'o';
sum[3]= 'u';
sum[5]= 't';
for(int i=0; i < 6; i++)
System.out.println(sum[i]);
}
}
Output:-
index6
a
b
o
u
t
Here index 4 value is skipped
Previous Code:-
Return
List Code:-
Java codes
Next Code:-
Initialization of arrays
Java Programs:-
List of Java Programs
Very Nice
ReplyDeleteIt’s always so sweet and also full of a lot of fun for me personally and my office colleagues to search your blog a minimum of thrice in a week to see the new guidance you have got.
ReplyDeletejava training in bangalore