簡體   English   中英

如何使用ArrayList制作一個表,該表保存繼承的類中的不同對象?

[英]How do I make a table with my ArrayList that holds different objects from inherited classes?

我正在做一個動物案例研究項目,在那里我制作了具有相同PIVS的不同繼承類。 我需要在格式正確的表格中打印代碼。 我的代碼:

public static void displayAll()
{
ArrayList <animals> animals= new ArrayList <animals>();
animals.add(new Tigers("Panthera tigris tigris","Asia", "The Bengal Tiger", "Tropical", 490, 7)); // Tigers
animals.add(new Tigers("Panthera tigris altaica","Asia", "The Siberian Tiger", "Woodlands", 400, 3));
animals.add(new Tigers("Panthera tigris sumatrae","Asia", "The Sumatran Tiger", "Tropical", 310, 5));
animals.add(new Tigers("Panthera tigris jacksoni","Asia", "The Malayan Tiger", "Tropical", 260, 5));
animals.add(new Tigers("Panthera tigris sondaica","Asia", "The Javan Tiger", "Tropical", 310, 2));
animals.add(new Tigers("Panthera tigris balica", "Asia", "The Bali Tiger", "Tropical", 200, 5));
animals.add(new Tigers("Panthera tigris virgata","Europe", "The Caspian Tiger", "Tropical", 530, 7));
animals.add(new Tigers("Panthera tigris amoyensis","Asia", "The South China Tiger", "Tropical", 330, 4));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Bulldog", "Temperate", 55, 2)); // Dogs
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Beagle", "Household", 24, 3));
animals.add(new Dogs("Canis lupus familiaris", "Asia", "The Pug", "Household", 20, 5));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Boxer", "Wild", 71, 4));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Poodle", "Household", 9, 2));
animals.add(new Dogs("Canis lupus familiaris", "Asia", "The Golden Retriever", "Tropical", 75, 3));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The German Shepherd", "Temperate", 88, 3));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Rottweiler", "Wild", 130, 2));
animals.add(new Elephants("Elephant maximus", "Asia", "The Asian Elephant", "Tropical", 12000, 1)); // Elephants
animals.add(new Elephants("Elephant maximus indicus", "Asia", "The Indian Elephant", "Tropical", 12000, 12));
animals.add(new Elephants("Loxodonta africana", "Africa", "The African Bush Elephant", "Savvanah", 13000, 6));
animals.add(new Elephants("Loxodonta cyclotis", "Africa", "The African Forest Elephant", "Tropical", 60000, 2));
animals.add(new Elephants("Elephas maximus borneensis", "Asia", "The Borneo Elephant", "Tropical", 14000, 8));
animals.add(new Elephants("Elephas maximus maximus", "Asia", "The Sri Lankan Elephant", "Woodlands", 5500, 5));
animals.add(new Elephants("Palaeoloxodon antiquus", "Asia", "The Straight Tusked Elephant", "Temperate", 22600, 8));
animals.add(new Elephants("Palaeoloxodon cyrpriotes", "Asia", "The Cyprus Dwarf Elephant", "Tropical", 440, 3));
animals.add(new Seals("Mirounga", "North America", "The Elephant Seal", "Aquatic", 5000, 2)); // Seals
animals.add(new Seals("Phoca vitulina", "North America", "The Harbor Seal", "Aquatic", 130, 3));
animals.add(new Seals("Pagophilus groenlandicus", "North America", "The Harp Seal", "Artic", 290, 1));
animals.add(new Seals("Halichoerus grypus", "North America", "The Grey Seal", "Aquatic", 550, 3));
animals.add(new Seals("Pusa hispida", "Asia", "The Ringed Seal", "Artic", 160, 4));
animals.add(new Seals("Phoca largha", "North America", "The Spotted Seal", "Aquatic", 160, 3));
animals.add(new Seals("Hydrurga leptonyx", "Antarctica", "The Leopard Seal", "Artic", 780, 2));
animals.add(new Seals("Monachini", "North America", "The Monk Seal", "Aquatic", 600, 2));
animals.add(new Whales("Orcinus orca", "North America", "The Killer Whale", "Aquatic", 12000, 1)); // Whales
animals.add(new Whales("Delphinapterus", "North America", "The Beluga Whale", "Aquatic", 3000, 3));
animals.add(new Whales("Monodon monoceros", "Asia", "The Narwhal Whale", "Aquatic", 2000, 5));
animals.add(new Whales("Balaenoptera musculus", "Antarctica", "The Blue Whale", "Artic", 300000, 3));
animals.add(new Whales("Megaptera novaeangliae", "North America", "The Humpback Whale", "Aquatic", 66000, 5));
animals.add(new Whales("Livyatan melvillei", "North America", "The Livyatan Whale", "Aquatic", 100000, 2));
animals.add(new Whales("Eschrichtius robustus", "North America", "The Gray Whale", "Aquatic", 60000, 18));
animals.add(new Whales("Balaenoptera physalus", "Asia", "The Fin Whale", "Aquatic", 100000, 7));
}

我試過的

我試圖使用轉義序列和for循環來打印我的代碼。

System.out.println("Press Button To Display Animals");
scanner = new Scanner(System.in);
setScanner(new Scanner(scanner.nextLine()));
System.out.println(String.format("%-10s%-10s%-%10s-%10s-%10s-10s", " Scientific Name ", " Continent Of Orgin ", " Name, "Natural Habitat", "Weight", " Num Of Offspring"));

for (Animals a : animals)
System.out.println(String.format("%-10s%-10s%-10d", a.getscientificname(), a.getcontinentoforgin(), a.getname(), a.getnaturalhabitat(), a.getweight(), a.getnumofoffspring()));

它給我很多錯誤

同樣,為了獲得獎金,我將如何找到一種搜索我的動物數組列表的方法,以找到所有亞洲本地動物?

所有幫助將不勝感激

public  void displayAll(){
    List<Animals> animals= new ArrayList();
    animals.add(new Tiger("Panthera tigris tigris","Asia", "The Bengal Tiger", "Tropical", 490, 7));
    animals.add(new Dog("Panthera tigris tigris","Asia", "The Bengal Tiger", "Tropical", 490, 7));

    for(Animals a:animals){
        System.out.println(String.format("name:%s,weight:%d",a.getName(),a.getWeight()));
    }
}

打印:

name:Panthera tigris tigris,weight:490
name:Panthera tigris tigris,weight:490

第一個明顯的問題(錯誤會有所幫助)。

ArrayList <animals> animals= new ArrayList <animals>();
...
for (Animals a : animals)

類型名稱已將大小寫從animals更改為Animals

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM