how to create an array in java
int[] array1 = new int[5]; //int array length 5
String[] array2 = new String[5] //String array length 5
double[] array3 = new double[5] // Double array length 5
how to create an array in java
int[] array1 = new int[5]; //int array length 5
String[] array2 = new String[5] //String array length 5
double[] array3 = new double[5] // Double array length 5
creating array of objects in java
class Main{
public static void main(String args[]){
//create array of employee object
Employee[] obj = new Employee[2] ;
//create & initialize actual employee objects using constructor
obj[0] = new Employee(100,"ABC");
obj[1] = new Employee(200,"XYZ");
//display the employee object data
System.out.println("Employee Object 1:");
obj[0].showData();
System.out.println("Employee Object 2:");
obj[1].showData();
}
}
//Employee class with empId and name as attributes
class Employee{
int empId;
String name;
//Employee class constructor
Employee(inteid, String n){
empId = eid;
name = n;
}
public void showData(){
System.out.print("EmpId = "+empId + " " + " Employee Name = "+name);
System.out.println();
}
}
java initialize object array
import java.util.stream.Stream;
class Example {
public static void main(String[] args) {
int len = 5; // For example.
// Use Stream to initialize array.
Foo[] arr = Stream.generate(() -> new Foo(1)) // Lambda can be anything that returns Foo.
.limit(len)
.toArray(Foo[]::new);
}
// For example.
class Foo {
public int bar;
public Foo(int bar) {
this.bar = bar;
}
}
}
java array object
Class obj[]= new Class[array_length]
java array object
class ObjectArray{
public static void main(String args[]){
Account obj[] = new Account[2] ;
//obj[0] = new Account();
//obj[1] = new Account();
obj[0].setData(1,2);
obj[1].setData(3,4);
System.out.println("For Array Element 0");
obj[0].showData();
System.out.println("For Array Element 1");
obj[1].showData();
}
}
class Account{
int a;
int b;
public void setData(int c,int d){
a=c;
b=d;
}
public void showData(){
System.out.println("Value of a ="+a);
System.out.println("Value of b ="+b);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us