简体   繁体   English

我的ArrayList对象没有打印出我想要的Object属性

[英]My ArrayList of Objects isn't printing out the Object attribute I want

I have an ArrayList of Objects. 我有一个对象的ArrayList。 The Objects are flowers. 对象是花。 After adding flowers to a list of flowers, I want to print out each flower's name. 将花朵添加到花朵列表之后,我想打印出每朵花朵的名称。 Whenever I add a flower, I give the flower a name. 每当我添加一朵花时,我都会给花起一个名字。 Say I add three flowers named "Rose", "Tulip", and "Daffodil". 假设我添加了三朵花,分别命名为“玫瑰”,“郁金香”和“水仙花”。 When I run my function that iterates through the ArrayList, all I get is three Daffodils. 当我运行遍历ArrayList的函数时,我得到的只是三个水仙花。 If I add five flowers, it displays five flowers but all of them have the name of the last flower I added. 如果我添加五朵花,它将显示五朵花,但是所有花都具有我添加的最后一朵花的名称。 Here is my main class: 这是我的主要课程:

import java.util.Scanner;
import java.util.ArrayList;

public class mainClass {

    static ArrayList<flowerClass> flowerPack = new ArrayList<flowerClass>();


    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        while(true){
            System.out.println("1. Add flower to flowerpack.");
            System.out.println("2. Display the flowers in the flowerpack.");

            int userChoice = input.nextInt();

            switch(userChoice){
            case 1:
                addFlower();
                break;
            case 2:
                displayFlowers();
                break;
            }
        }
    }

    public static void addFlower(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the flower's name?");
        String flowerName = input.nextLine();
        flowerPack.add(new flowerClass(flowerName));
    }

    public static void displayFlowers(){

        for (Object o: flowerPack){
            System.out.println(flowerClass.getName());
        }
    }
}

Here is my flowerClass: 这是我的flowerClass:

    public class flowerClass {

    public static int numberOfFlowers = 0;
    public static String flowerName = null;
    static String flowerColor = null;
    static int numberThorns = 0;
    static String flowerSmell = null;

    flowerClass(String desiredName){
        flowerName = desiredName;
        numberOfFlowers++;
    }

    public static void setName(String desiredName){
        flowerName = desiredName;
    }

    public static String getName(){
        return flowerName;
    }
}

I have tried using an array of objects instead of an ArrayList. 我尝试使用对象数组而不是ArrayList。 I can get it to display the unique address of each separate flowerClass object but I can never successfully access each object's flowerName to display it to the user. 我可以获得它来显示每个单独的flowerClass对象的唯一地址,但是我永远无法成功访问每个对象的flowerName来将其显示给用户。 Thanks for any suggestions. 感谢您的任何建议。

A couple of things. 有几件事。 First, you should not set this as static: 首先,您不应将此设置为静态:

public static String flowerName = null;

As a static variable, all instances of flowerClass will have the same flowerName. 作为静态变量,flowerClass的所有实例将具有相同的flowerName。 It should be: 它应该是:

public String flowerName;  // null is implicit

Second, your loop: 二,你的循环:

for (Object o: flowerPack){
    System.out.println(flowerClass.getName());
}

What you are doing here is calling getName() on the class flowerClass, not an instance of flowerClass. 您在这里所做的是在flowerClass类上调用getName() ,而不是flowerClass的实例。 They only reason this even compiles is because you marked those methods as static. 他们之所以这样做,甚至只是编译是因为您将这些方法标记为静态。

Remove static from your getter and setter (again you want to access the instance of flowerClass, not the class flowerClass): 从getter和setter中删除static(同样,您想访问flowerClass的实例 ,而不是class flowerClass的实例 ):

public void setName(String desiredName){
    flowerName = desiredName;
}

public String getName(){
    return flowerName;
}

Then iterate over the instances of flowerClass: 然后遍历flowerClass的实例

for (flowerClass flower: flowerPack){
     System.out.println(flower.getName());
}

This is because your setter and getter methods as well as the member variable 'name' are static. 这是因为您的setter和getter方法以及成员变量“ name”是静态的。 So whenever you set flower's name, you are basically overwriting the previous flower's name. 因此,无论何时设置花朵的名称,基本上都将覆盖前一朵花朵的名称。 Since there is only one name for all the objects. 因为所有对象只有一个名称。 All your objects basically point to the same name. 您的所有对象基本上都指向相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM