“Array objek” Kode Jawaban

Java Class Array of Objects

public class MainClass
{  
    public static void main(String args[])
    {
        System.out.println("Hello, World!");
        //step1 : first create array of 10 elements that holds object addresses.
        Emp[] employees = new Emp[10];
        //step2 : now create objects in a loop.
        for(int i=0; i<employees.length; i++){
            employees[i] = new Emp(i+1);//this will call constructor.
        }
    }
}

class Emp{
    int eno;
    public Emp(int no){
        eno = no;
        System.out.println("emp constructor called..eno is.."+eno);
    }
}
Witty Wildebeest

Array objek

class Police
{
	public int id;
	public String name;
	Police(int id, String name)
	{
		this.id = id;
		this.name = name;
	}
}
class PoliceClass
{
	public static void main (String[] args)
	{
		
		Police[] array;
		array = new Police[4];
		array[0] = new Police(3242,"Peter");
		array[1] = new Police(4353,"Adil");
		array[2] = new Police(6433,"Preet");
		array[3] = new Police(3434,"Ranjeet");

		for (int i = 0; i < array.length; i++)
			System.out.println("Police array Element at " + i + " : " +
						array[i].id +" "+ array[i].name);

}
}
Unsightly Unicorn

Jawaban yang mirip dengan “Array objek”

Pertanyaan yang mirip dengan “Array objek”

Lebih banyak jawaban terkait untuk “Array objek” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya