Java 访问对象数组中的 object.variable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3123086/
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
Access object.variable in an array of objects
提问by theTuxRacer
I need help with this piece of code.
我需要这段代码的帮助。
public class ParkingLot {
static int MAX = 5;
static Car[] Slot = new Car[MAX];
public static void main(String[] args) {
Slot[0] = new Car("1234", "White");
Slot[1] = new Car("5678", "Black");
}
public static void Allot() {
for (int i = 0; i <= Slot.length; i++) {
System.out.println(Slot.getNo);
}
}
I am storing a Car
Object in Slot
. I wish to print/access the No
and Colour
of the car stored in slot. How do I go about doing that?
我Car
在Slot
. 我希望打印/访问存储在插槽中的汽车的No
和Colour
。我该怎么做?
采纳答案by Bozho
Well, if car
has a public property, or a public getter method (this is preferable - getNumber()
and getColour()
), you can call them while iterating the array with the for-each loop:
好吧,如果car
有公共属性或公共 getter 方法(最好是 -getNumber()
和getColour()
),您可以在使用 for-each 循环迭代数组时调用它们:
for (Car car : slot) {
System.out.println(car.getColour());
}
Note that I've lowercased slot
- variable names in Java should be lowercase. I'd also advise for naming the array with plural name - i.e. slots
.
请注意,我已经小写了slot
- Java 中的变量名应该是小写的。我还建议使用复数名称命名数组 - 即slots
.
Note also that the way of iteration provided by others is possible, but not recommended for the basic case of iterating the whole array. Effective Java (Bloch)recommends using the foreach loop whenever possible.
另请注意,其他人提供的迭代方式是可能的,但不推荐用于迭代整个数组的基本情况。Effective Java (Bloch)建议尽可能使用 foreach 循环。
回答by T.J. Crowder
Using []
notation:
使用[]
符号:
public static void Allot() {
Car car;
for (int i = 0; i <= Slot.length; i++) {
// Get the car at this position in the array
car = Slot[i];
// Make sure it isn't null, since the array may not have
// a full set of cars
if (car != null) {
// Use the car reference
System.out.println(car.getNo());
}
}
}
(I assumed by the name that getNo
was a method, not a property.)
(我假设名称getNo
是方法,而不是属性。)
E.g., Slot[0]
gives you the first Car
, from which you can access Car
's properties and methods, so Slot[i]
gives you the car at the i
th position. (In the above I used a temporary variable to store the car, but you can use Slot[i].getNo()
directly, it doesn't matter. I just didn't want to repeat the array lookup, even through HotSpot [the Sun JVM] will optimize it out even if I do.)
例如,Slot[0]
为您提供 first Car
,您可以从中访问Car
的属性和方法,因此Slot[i]
在i
th 位置为您提供汽车。(上面我用了一个临时变量来存储汽车,但你可以Slot[i].getNo()
直接使用,没关系。我只是不想重复数组查找,即使通过HotSpot [the Sun JVM] 也会优化出来即使我这样做。)
回答by Badar
Sorry for being so late. I noticed something missing in the above answers, so here is the complete solution for the problem stated.
抱歉这么晚了。我注意到上面的答案中缺少一些东西,所以这里是所述问题的完整解决方案。
Here is the ParkingLot class with a call to Allot() method.
这是调用 Allot() 方法的 ParkingLot 类。
public class ParkingLot {
公共类停车场{
static int MAX = 5;
static Car[] Slot = new Car[MAX];
public static void main(String[] args) {
Slot[0] = new Car("1234", "White");
Slot[1] = new Car("5678", "Black");
Allot();
}
public static void Allot() {
for (int i = 0; i < Slot.length; i++) {
if (Slot[i] != null) {
System.out.println(Slot[i].getNo()+" , "+Slot[i].getColor());
}
}
}
}
}
And the Car class with the getNo() and getColor() methods.
以及带有 getNo() 和 getColor() 方法的 Car 类。
public class Car {
公共类汽车{
private String Number;
private String Color;
Car (String Number, String Color){
this.Number = Number;
this.Color = Color;
}
public String getNo(){
return Number;
}
public String getColor(){
return Color;
}
}
}
回答by Arpit Agrawal
class Car{
String number;
String color;
public Car(String number, String color) {
this.number = number;
this.color = color;
}
@Override
public String toString() {
return "Car{" +
"number='" + number + '\'' +
", color='" + color + '\'' +
'}';
}
}
class Test{
static int MAX = 5;
static Car[] Slot = new Car[MAX];
public static void main(String[] args) {
Slot[0] = new Car("1234", "White");
Slot[1] = new Car("5678", "Black");
for (Car car : Slot)
System.out.println(car);
}
}
回答by Aylian Craspa
you can create and access object array of objects simply like this
您可以像这样简单地创建和访问对象的对象数组
Object[] row={"xx","xcxcx"};
Object[] cotainer = {row,row,row};
for(int a=0;a<cotainer.length;a++){
Object[] obj = (Object[])cotainer[a];
}