[英]How to access array of objects with specific indexes from a different class in Java?
我知道已經有很多問題與我的類似,但我仍然沒有找到適用於我的問題的解決方案。 我需要為此使用三個不同的類並給你一個想法,這是我的一些代碼的樣子(僅包括我認為相關的代碼):
public class Main {
//this is where I instantiate
public static void main(String[] args){
Habitat habitat1 = new Habitat(some arguments/parameters here);
Habitat habitat2 = new Habitat(some arguments/parameters here);
Animal type1 = new Animal(some arguments/parameters here);
Animal type2 = new Animal(some arguments/parameters here);
Animal type3 = new Animal(some arguments/parameters here);
Animal type4 = new Animal(some arguments/parameters here);
}
}
public class Habitat {
//other attributes here
Animal[] animals;
public Habitat(some arguments/parameters here) {
//here I want to be able to reference/assign **Animal type1 and type2 to habitat1** and **Animal type3 and type4 to habitat2**
}
public class Animal {
//other attributes here
Habitat habitat;
public Animal(some arguments/parameters here){
}
我知道為了擁有 object 動物的數組,我需要這樣寫:
Animal[] animals = new Animal[4];
animals[0] = type1;
animals[1] = type2;
animals[2] = type3;
animals[3] = type4;
問題是我不能把這段代碼放在class Habitat,因為它不能訪問type1-type4。 當我把它放在 class Main 中時,我無法在 class Habitat 中引用它。
基本上,我在這個特定問題中想要的具體 output 是當我打印 habitat1 的內容時,例如,它看起來像這樣:
Habitat 1 - Savanna
Mixed woodland-grassland
List of Animals:
Type 1
Name: Tiger
Diet: Carnivore
Type 2
Name: Elephant
Diet: Herbivore
添加一個方法addAnimal( Animal animal )
到Habitat
並像這樣從Animal
的構造函數中調用它
…
habitat.addAnimal( this );
…
當然,這假設Animal
構造函數的第四個參數名為habitat
。
animals
屬性不應該是數組,而是java.util.List
的實例(最好是java.util.ArrayList
)。 那么新方法將如下所示:
public final void addAnimal( final Animal animal )
{
animals.add( animal );
}
也許你必須添加一些檢查(例如,not-null),並且當你想確保只可以添加一次動物時,你應該考慮使用java.util.Set
而不是List
(但這會也意味着對Animal
class 的一些更改)。
如果您真的堅持為屬性使用數組,則addAnimal()
方法會變得有點復雜:
public final void addAnimal( final Animal animal )
{
if( animals == null )
{
animals = new Animals [1];
animals [0] = animal;
}
else
{
var curLen = animals.length;
var newAnimals = new Animal [curLen + 1];
System.arraycopy( animals, 0 newAnimals, 0, curLen )
animals = newAnimals;
animals [curLen] = animal;
}
}
或者
public final void addAnimal( final Animal animal )
{
if( animals == null )
{
animals = new Animals [1];
animals [0] = animal;
}
else
{
var curLen = animals.length;
animals = Arrays.copyOf( animals, curLen + 1 );
animals [curLen] = animal;
}
}
仍然缺少錯誤處理。
在Habitat
上提供用於關聯Animal[]
的方法。
例如,如果您定義一個接受Animal[]
的構造函數,那么您可以先構建該數組,然后將其傳遞給構造函數。 您還可以提供一個 setter,允許您隨時切換到不同的Animal[]
。
public class Habitat {
private Animal[] animals; // note that this should be private
public Habitat(Animal[] animals) {
this.animals = animals;
}
public void setAnimals(Animal[] animals) {
this.animals = animals;
}
}
這允許 main 方法預先生成 Animals 或稍后根據需要分配它們。
public class Main {
public static void main(String[] args) {
Animal[] animals = ... build the array
Habitat habitat1 = new Habitat(animals);
Habitat habitat2 = new Habitat();
Animal[] moreAnimals = ... build another array of Animals
habitat2.setAnimals(moreAnimals);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.