1int[] data = {10,20,30,40,50,60,71,80,90,91};
2// or
3int[] data;
4data = new int[] {10,20,30,40,50,60,71,80,90,91};
5// or
6int[] data = new int[10];
7data = {10,20,30,40,50,60,71,80,90,91};
1obj array[] = new obj[10];
2
3for (int i = 0; i < array.length; i++) {
4 array[i] = new obj(i);
5}
1class Main{
2 public static void main(String args[]){
3 //create array of employee object
4 Employee[] obj = new Employee[2] ;
5
6 //create & initialize actual employee objects using constructor
7 obj[0] = new Employee(100,"ABC");
8 obj[1] = new Employee(200,"XYZ");
9
10 //display the employee object data
11 System.out.println("Employee Object 1:");
12 obj[0].showData();
13 System.out.println("Employee Object 2:");
14 obj[1].showData();
15 }
16}
17//Employee class with empId and name as attributes
18class Employee{
19 int empId;
20 String name;
21 //Employee class constructor
22 Employee(inteid, String n){
23 empId = eid;
24 name = n;
25 }
26public void showData(){
27 System.out.print("EmpId = "+empId + " " + " Employee Name = "+name);
28 System.out.println();
29 }
30}
1import java.util.stream.Stream;
2
3class Example {
4
5 public static void main(String[] args) {
6 int len = 5; // For example.
7
8 // Use Stream to initialize array.
9 Foo[] arr = Stream.generate(() -> new Foo(1)) // Lambda can be anything that returns Foo.
10 .limit(len)
11 .toArray(Foo[]::new);
12 }
13
14 // For example.
15 class Foo {
16 public int bar;
17
18 public Foo(int bar) {
19 this.bar = bar;
20 }
21 }
22}
23
1class ObjectArray{
2 public static void main(String args[]){
3 Account obj[] = new Account[2] ;
4 //obj[0] = new Account();
5 //obj[1] = new Account();
6 obj[0].setData(1,2);
7 obj[1].setData(3,4);
8 System.out.println("For Array Element 0");
9 obj[0].showData();
10 System.out.println("For Array Element 1");
11 obj[1].showData();
12 }
13}
14class Account{
15 int a;
16 int b;
17 public void setData(int c,int d){
18 a=c;
19 b=d;
20 }
21 public void showData(){
22 System.out.println("Value of a ="+a);
23 System.out.println("Value of b ="+b);
24 }
25}