Example 1: how to initialize an array in java
int[] data = {10,20,30,40,50,60,71,80,90,91};
int[] data;
data = new int[] {10,20,30,40,50,60,71,80,90,91};
int[] data = new int[10];
data = {10,20,30,40,50,60,71,80,90,91};
Example 2: array in java
int[] age = new int[3];
age[0] = 1;
age[1] = 3;
age[2] = 6;
for (int i=0; i < 3; i++)
System.out.println(age[i]);
int[] num = {3,3,5};
System.out.println(num[0]);
Example 3: arrays in java
int[] intArray = new int[20];
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Example 4: array objects java
obj array[] = new obj[10];
for (int i = 0; i < array.length; i++) {
array[i] = new obj(i);
}
Example 5: java array object
Class obj[]= new Class[array_length]
Example 6: java class array of objects
public class MainClass
{
public static void main(String args[])
{
System.out.println("Hello, World!");
Emp[] employees = new Emp[10];
for(int i=0; i<employees.length; i++){
employees[i] = new Emp(i+1);
}
}
}
class Emp{
int eno;
public Emp(int no){
eno = no;
System.out.println("emp constructor called..eno is.."+eno);
}
}