有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我如何用我的ArrayList创建一个包含继承类中不同对象的表?

我正在做一个动物案例研究项目,在这个项目中,我制作了具有相同PIV的不同遗传类。我需要将代码打印在一个格式良好的表格中。 我的代码:

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()));

它给了我很多错误

另外,为了奖励,我将如何找到一种方法来搜索我的动物列表,以找到所有原产于亚洲的动物

一切帮助都将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    第一个明显的问题(这些错误会有帮助)

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

    类型名称的大小写已从animals更改为Animals

  2. # 2 楼答案

    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