“Java -Array -Objekte” Code-Antworten

Java -Klassenarray von Objekten

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

Java -Array -Objekte

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

Ähnliche Antworten wie “Java -Array -Objekte”

Fragen ähnlich wie “Java -Array -Objekte”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen