Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Hondo Chalthoum    on Sep 23 In Java Category.

  
Question Answered By: Angel Watkins   on Sep 23

Animal[] animals = new Animal[200];
animals[0] = new Animal();
animals[1] = new Cheetah();
animals[2] = new Gazelle();
animals[3] = new Animal();
animals[4] = new Cheetah();
animals[5] = new Gazelle();
animals[6] = new Animal();
animals[7] = new Cheetah();
animals[8] = new Gazelle();

for (int i = 0; i < animals.length;i++)
if (animals[i] == null) {
System.out.println("Not initialized.");
} else{
if (animals instanceof Cheetah) System.out.println("animals[" + i
+ "] is a Cheetah");
if (animals instanceof Gazelle) System.out.println("animals[" + i
+ "] is a Gazelle");
if (animals instanceof Cheetah) System.out.println("animals[" + i
+ "] is an Animal");
}
}

Share: 

 
 
Didn't find what you were looking for? Find more on Classes in arrays Or get search suggestion and latest updates.


Tagged: