Java 如何用对象手动填充数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19725142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manually populate array with objects?
提问by Ilja
I'm new to java and am having issues understanding how to populate an array with object manually. Reason I wan't to do this manually is because I have 40 objects I need to create where 20 object go to arrayOne
and other 20 objects go to arrayTwo
. Also each object has a unique parameter like "Texas" or "Canada" that needs to be set.
我是 Java 新手,在理解如何手动使用对象填充数组时遇到问题。我不想手动执行此操作的原因是因为我需要创建 40 个对象,其中 20 个对象要去arrayOne
,其他 20 个对象要去arrayTwo
。此外,每个对象都有一个需要设置的唯一参数,例如“德克萨斯州”或“加拿大”。
I would usually create an array like this:
我通常会创建一个这样的数组:
long[] arrayOne;
arrayOne = new long[20];
and than populate it with, lets say numbers through a loop or manually. However now I am dealing with objects and am struggling to figure it out, I tried looking up answers here at StackOverflow, but was not able to understand what was going on there exactly.
而不是填充它,让我们通过循环或手动说出数字。但是现在我正在处理对象并且正在努力弄清楚,我尝试在 StackOverflow 上查找答案,但无法完全理解那里发生了什么。
If it helps, this is a constructor for my object
如果有帮助,这是我的对象的构造函数
// 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
采纳答案by ???v?т?
I would suggest using an ArrayList
instead of an array, because a list can grow but an array is a fixed size. However, to answer your question:
我建议使用一个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.
If you used an ArrayList
it would be:
如果您使用ArrayList
它,它将是:
List<Plane> arrayOne = new ArrayList<Plane>();
planes.add(new Plane(1001, "Timbuktu"));
planes.add(new Plane(2930, "Siberia"));
// etc.
Or, if you're really fancy:
或者,如果你真的很喜欢:
List<Plane> planes = new ArrayList<Plane>() {{
add(new Plane(1001, "Timbuktu"));
add(new Plane(2930, "Siberia"));
}};
In all cases you can iterate over the contents as follows:
在所有情况下,您都可以按如下方式迭代内容:
for (Plane plane : arrayOne) {
System.out.println(plane.getDestination());
}
回答by wxyz
U first create the Plane array:
你首先创建平面数组:
Plane[] planes = new Plane[20];
then each object:
然后每个对象:
planes[0] = new Plane(...);
...
...
回答by aga
Plane[] array = new Plane[10];
array[0] = new Plane(/*specify your parameters here*/)
Check out the chapter 10of Java Language Specification.
查看Java 语言规范的第 10 章。
回答by Mateusz
You have to declare an array of objects (in this case Plane
) just like you declare array of long
- Plane[] arrayOne = new Plane[20];
. Then you can access the elements using indices in the same manner. If you really have to populate it manually, you should do something like:
您必须声明一个对象数组(在本例中Plane
),就像您声明long
-数组一样Plane[] arrayOne = new Plane[20];
。然后,您可以以相同的方式使用索引访问元素。如果您真的必须手动填充它,您应该执行以下操作:
arrayOne[0] = new Plane(1, "foo", "bar", "baz", 1.0);
arrayOne[1] = new Plane(2, "fooo", "baar", "baaz", 2.0);
There are only two things that differ from the usage of Object[]
array from long[]
- type of the array and fact that at some point you have to use constructors to create objects. You can use a previously created object though.
只有两件事与Object[]
数组from 的用法不同long[]
-数组的类型以及在某些时候您必须使用构造函数来创建对象的事实。不过,您可以使用以前创建的对象。
回答by Mena
You can use a common interface
if the elements of your array
are not necessarily instances of Plane
.
如果您interface
的元素array
不一定是Plane
.
For instance:
例如:
package test;
包装测试;
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();
}
Output:
输出:
Weeee I'm flying!
Chirp chirp