java java如何创建不同对象的数组/矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4174543/
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
java how to create array/matrix of different objects
提问by ace
I'm a bit confused
我有点困惑
I created a class called person, that has an age and name attributes (and get set methods). Then in another class I want to create an array of persons , where each person has a different age and name. But some how in the end all of my persons end up with the last name and age. If I manually create them then it is ok, but with a for loop I've got that problem. What should I do to get different persons?
我创建了一个名为 person 的类,它有一个 age 和 name 属性(以及 get set 方法)。然后在另一个班级中,我想创建一个 people 数组,其中每个人都有不同的年龄和姓名。但有些人最终如何以姓氏和年龄结束我所有的人。如果我手动创建它们就可以了,但是使用 for 循环我就遇到了这个问题。我应该怎么做才能得到不同的人?
Here is the code of the person class:
这是person类的代码:
public class person {
static String name;
static int age;
public person() {
name="name";
age=0;
}
public static String getName() {
return name;
}
public static void setName(String name) {
person.name = name;
}
public static int getAge() {
return age;
}
public static void setAge(int age) {
person.age = age;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
}
And here is the code where I want to create my array/matrix:
这是我要创建数组/矩阵的代码:
public class array {
static person[][] a;
public static void main(String[] args) {
a=new person[3][3];
//manual created person
person first=new person();
person second=new person();
person third=new person();
first.setAge(12);
first.setName("first");
second.setAge(20);
second.setName("second");
third.setAge(40);
third.setName("third");
//automatic (here I get the disired effect)
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
a[i][j]=new person();
a[i][j].setAge(10+j);
a[i][j].setName("Alia"+i);
System.out.println(i+" "+j+" "+a[i][j].getName()+" "+a[i][j].getAge());
}
}
// a[0][0]=first;
// a[0][1]=second;
// a[1][2]=third;
// System.out.println(a[0][0].getName()+" "+a[0][0].getAge());
//for checking , and it doesnt work anymore
System.out.println(a[0][0].getName()+" "+a[0][0].getAge());
// for (int i = 0; i < a.length; i++) {
// for (int j = 0; j < a.length; j++) {
// System.out.println(i+" "+j+" "+a[i][j].getName()+" "+a[i][j].getAge());
// }
//
// }
getname();
}
private static void getname() {
System.err.println("get name function");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
System.out.println(a[i][j].getName());
}
}
}
}
回答by dacwe
Remove the static
keyword from the persons attributes. If it is staticit is used by all instances(all person objects).
static
从人员属性中删除关键字。如果它是静态的,则所有实例(所有人员对象)都使用它。
But I would do it like this:
但我会这样做:
public class Person {
public final String name;
public final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name + " (" + age + ")";
}
public static void main(String... args) {
List<Person> people = new LinkedList<Person>();
people.add(new Person("David", 28));
people.add(new Person("Andreas", 27));
System.out.println(people);
}
}
回答by Ulrich Schwarz
Yes, your attributes are declared static. A static attribute "belongs" to the class, not the instances, so all instances see the same String and int. You should be fine just removing the static from everything but main(). Then, new Person() will allocate new separate variables for everybody.
是的,您的属性被声明为静态的。静态属性“属于”类,而不是实例,因此所有实例都看到相同的 String 和 int。你应该没问题,只需从除 main() 之外的所有内容中删除静态。然后, new Person() 将为每个人分配新的单独变量。
回答by Amit S
Problem is the static fields. Last values assigned to them would be reflected in all the object.
问题是静态字段。最后分配给它们的值将反映在所有对象中。