简体   繁体   English

将对象放入另一个对象数组内部的对象数组

[英]Placing an Object into and Object Array, which is inside another Object Array

So I'm new to coding/Java and I'm working on this project. 所以我是编码/ Java的新手,正在从事这个项目。 My assignment is to create 3 different classes (which I have here) and make a them cooperate. 我的任务是创建3个不同的类(在这里有),并使它们配合。 I've successfully made my FishTankManagerApp class retrieve a new Fish object and I'm trying to figure out how to put it in a FishTank object. 我已经成功地使FishTankManagerApp类检索了一个新的Fish对象,并且试图找出如何将其放入FishTank对象中。

My FishTank class only is only there to create an array object which can hold 5 fish (I think I've done this correctly). 我的FishTank类仅用于创建一个可以容纳5条鱼的数组对象(我认为我已经正确地做到了)。 In my FishTankManagerApp class, I've created an array of 10 of these FishTank Objects. 在我的FishTankManagerApp类中,我创建了一个由10个FishTank对象组成的数组。

My question which I cant figure out for the life of me is how do I place the Fish objects into a specfic FishTank object after they've been created (I've made a note at the end of the code where I've ran into a problem). FishTank我的问题是,在创建对象后,如何将Fish对象放入特定的FishTank对象中(我在遇到的代码末尾做了一个注释一个问题)。

Essentially I know I'm trying to put an object I've created inside of and array which contains another array where fish objects can be stored... I think.... 从本质上讲,我知道我想将我创建的对象放入and数组中,该对象包含另一个可以存储鱼对象的数组...我认为....

Any help would be much appreciated! 任何帮助将非常感激! Thank you! 谢谢!

import java.util.Scanner;

public class Fish {

    private static Scanner stdin = new Scanner(System.in);
    private String userInput;
    private int userInput2;
    public boolean mean;
    public String name;

    public Fish(){      
        System.out.println("What is your fishes name?");

        userInput = stdin.next();
        this.name = userInput;

        System.out.println("Is this fish aggressive?\n"+
                           "(1)Yes\n(2)No");

        userInput2 = stdin.nextInt();
        if (userInput2 == 1)
            this.mean = true;
        else
            this.mean = false;
    }
}
public class FishTank {

    public FishTank(){

        Fish[] tank = new Fish[5];

    }
}
import java.util.Scanner;

public class FishTankManager {

    private static Scanner stdin = new Scanner(System.in);
    private static int userInput;
    private static FishTank[] tanks = new FishTank[10];

    public static void main (String[] args) {


        while (true){
            System.out.println("Welcome to your new fish tank manager!\n"+
                               "What would you like to do?\n"+
                               "(1)Add a Fish\n(2)Move a Fish\n"+
                               "(3)Check a tank");          
            userInput = stdin.nextInt();

            if (userInput == 1){
                Fish fish = new Fish();     
                System.out.println(fish.name);
                System.out.println(fish.mean);  

                changeTank(fish);

            }
            else if(userInput ==2){
            }
            else{
            }
        }
    }

    private static void changeTank(Fish fish){

        System.out.println("Which tank would you like to put this fish in? (1-10)");
        userInput = stdin.nextInt();

        tanks[userInput] = fish;
        // This last line is where I'm confused on how to put the fish() object into a tank which I've created.
    }
}

I'd recommend adding a method to your FishTank to make adding the fish easy. 我建议您在FishTank添加一种方法,以使添加鱼变得容易。 Maybe something like this: 也许是这样的:

public FishTank(){

    private Fish[] tank = new Fish[5];

    public boolean addFish(Fish fish) {
        // ... add code here to put the fish to the tank array
        // return true if there was room in the tank, false otherwise
    }
}

Note that the tank variable is now private. 注意, tank变量现在是私有的。 It's generally a good idea to keep member variables private in Java, and use methods to access them. 通常,在Java中将成员变量保持私有状态并使用方法来访问它们是一个好主意。

Then, where you've got the comment you mentioned, you can just call the new method: 然后,在提及的地方,您可以调用新方法:

boolean addedSuccessfully = tanks[userInput].addFish(fish);

You may not need to return the boolean as I'm showing, but it might be handy if you need to check whether the array (ie the tank) had room for the new fish. 您可能不需要像我展示的那样返回布尔值,但是如果您需要检查数组(即水箱)是否有空间容纳新鱼,这可能会很方便。

Currently, what you're doing is setting the FishTank object equal to a Fish instance - not adding it to the tank variable inside of FishTank. 当前,您正在做的是将FishTank对象设置为等于Fish实例-而不是将其添加到FishTank内部的tank变量中。

Right now, you have no way of accessing the tank variable inside of the FishTank class. 现在,您无法访问FishTank类内部的tank变量。 What you need to do is make it a global variable and provide accessor/modifier methods. 您需要做的是使其成为全局变量并提供访问器/修饰符方法。 For example: 例如:

public class FishTank {

private Fish[] tank;
private int numFish;

public FishTank(){

    this.tank = new Fish[5];
    this.numFish = 0;

}

public void add(Fish f){
    if(this.numFish >= 5){
        //handle "tank is full" case
    }
    else{
        numFish++;
        this.tank[numFish] = f;
    }
}
}

Then invoke add on the desired FishTank object: 然后在所需的FishTank对象上调用add:

tanks[userInput].add(fish); tanks [userInput] .add(fish);

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

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