Java 如何用对象手动填充数组?

Java 如何用对象手动填充数组?,java,arrays,object,Java,Arrays,Object,我是java新手,在理解如何手动使用对象填充数组时遇到问题。我不想手动执行此操作的原因是,我需要创建40个对象,其中20个对象转到arrayOne,其他20个对象转到arrayTwo。此外,每个对象都有一个需要设置的唯一参数,如“Texas”或“Canada” 我通常会创建如下数组: long[] arrayOne; arrayOne = new long[20]; 然后通过循环或手动方式填充数字。然而,现在我正在处理对象,并且正在努力找出它,我尝试在StackOverflow这里查找答案,但

我是java新手,在理解如何手动使用对象填充数组时遇到问题。我不想手动执行此操作的原因是,我需要创建40个对象,其中20个对象转到
arrayOne
,其他20个对象转到
arrayTwo
。此外,每个对象都有一个需要设置的唯一参数,如“Texas”或“Canada”

我通常会创建如下数组:

long[] arrayOne;
arrayOne = new long[20];
然后通过循环或手动方式填充数字。然而,现在我正在处理对象,并且正在努力找出它,我尝试在StackOverflow这里查找答案,但无法准确理解那里发生了什么

如果有帮助,这是我的对象的构造函数

    // Plane Constructor
    public Plane (int i, String dest, String airl, String airc, double t) {

            planeID = i;
            destination = dest;
            airline = airl;
            aircraft = airc;
            time = t;

    }// END Plane Constructor

U首先创建平面阵列:

Plane[] planes = new Plane[20];
然后每个对象:

planes[0] = new Plane(...);


查看Java语言规范的列表。

我建议使用
ArrayList
而不是数组,因为列表可以增长,但数组的大小是固定的。但是,要回答您的问题:

Plane[] arrayOne = new Plane[20];
Plane[] arrayTwo = new Plane[20];

arrayOne[0] = new Plane(1001, "Timbuktu");
arrayOne[1] = new Plane(2930, "Siberia");
// etc.

arrayTwo[0] = new Plane(2019, "France");
arrayTwo[1] = new Plane(1222, "Italy");
// etc.
如果使用
ArrayList
,它将是:

List<Plane> arrayOne = new ArrayList<Plane>();
planes.add(new Plane(1001, "Timbuktu"));
planes.add(new Plane(2930, "Siberia"));
// etc.

您必须声明对象数组(在本例中为
Plane
),就像声明
long
-
Plane[]arrayOne=newplane[20]的数组一样。然后,您可以以相同的方式使用索引访问元素。如果确实需要手动填充,则应执行以下操作:

arrayOne[0] = new Plane(1, "foo", "bar", "baz", 1.0);
arrayOne[1] = new Plane(2, "fooo", "baar", "baaz", 2.0);

使用
Object[]
数组与使用
long[]
数组只有两个不同之处,即数组的类型以及在某些情况下必须使用构造函数来创建对象。但您可以使用以前创建的对象

如果
数组
的元素不一定是
平面
的实例,则可以使用公共
界面

例如:

包装试验

public class Main {
    public static void main(String[] args) {
        Flyer[] flyers = new Flyer[] { new Plane(), new Bird() };
            for (Flyer f: flyers) {
                // you can only access method "fly" here, because it's the only
                // method defined in your interface, but nothing
                // stops you from adding more methods, as long as you implement 
                // them in the (non-abstract) classes
                f.fly();
            }
    }
}

class Plane implements Flyer {
    // TODO id, destination, airline, etc. getters/setters
    @Override
    public void fly() {
        System.out.println("Weeee I'm flying!");
    }
}

class Bird implements Flyer {
    // TODO whatever properties / getters / setters
    @Override
    public void fly() {
        System.out.println("Chirp chirp");
    }
}

interface Flyer {
    void fly();
}
输出:

Weeee I'm flying!
Chirp chirp

这是一个非常丑陋的解决方案。如果你不知道你将从某处拥有/检索多少对象,该怎么办?动态数组应该更有效率。@Sajom,OP说他们是Java新手。可能还没有学会数据结构。您需要知道如何使用数组。所以在他们跑之前让他们先走。此外,OP明确表示它们有40个对象,每个数组20个。@Sajmon如果我看到“如何在Java中使用数组?”这个问题,我将展示Java中的数组示例。我不理解有人对新手说“不,不,这是错误的方法,使用ArrayList,将其分配给列表接口,以便您可以在以后更改实现,并考虑一些您可以在这里使用的模式”。OP让他解释Java的基础知识,我做了。如果他需要了解ArrayList,他会问另一个问题,或者在其他地方学习。但你们在这里建议最好的方法,或者至少提到还有另一种更有效的方法,我认为使用静态或动态数组几乎是一样的,也许使用动态数组更简单、更舒适。但好吧,我刚才提到了——没有冒犯的意思。你可以两者兼得;回答这个问题并分享你的智慧。非常感谢你进一步的研究,但我认为这样的东西还不是我的水平,我不想先学习基础知识,但我一定会在后面研究@伊利亚:当然!我可能误解了你的问题:)
arrayOne[0] = new Plane(1, "foo", "bar", "baz", 1.0);
arrayOne[1] = new Plane(2, "fooo", "baar", "baaz", 2.0);
public class Main {
    public static void main(String[] args) {
        Flyer[] flyers = new Flyer[] { new Plane(), new Bird() };
            for (Flyer f: flyers) {
                // you can only access method "fly" here, because it's the only
                // method defined in your interface, but nothing
                // stops you from adding more methods, as long as you implement 
                // them in the (non-abstract) classes
                f.fly();
            }
    }
}

class Plane implements Flyer {
    // TODO id, destination, airline, etc. getters/setters
    @Override
    public void fly() {
        System.out.println("Weeee I'm flying!");
    }
}

class Bird implements Flyer {
    // TODO whatever properties / getters / setters
    @Override
    public void fly() {
        System.out.println("Chirp chirp");
    }
}

interface Flyer {
    void fly();
}
Weeee I'm flying!
Chirp chirp