对如何实现 ArrayList 感到困惑
Confused on how to implement an ArrayList
所以我在查看 ArrayLists 时对如何向数组添加元素感到困惑,而没有为每个对象一遍又一遍地明确说明 people.add(person1)。我目前编译的这段代码没有在控制台中打印任何内容。我认为我的错误与对象构造函数和 people.add(this)
有关。我做错了什么?
import java.util.ArrayList;
public class People {
int age;
String name;
static ArrayList<People> people = new ArrayList<People>();
public People(String name, int age){
this.name = name;
this.age = age;
people.add(this);
}
People person1 = new People("Bob", 41);
People person2 = new People("Arthur", 32);
People person3 = new People("Tom",18);
public static void main(String[] args){
for(People p : people) {
System.out.println(p.name);
}
}
}
数组列表是一个 static
变量。您正在创建的 People
对象是非静态变量,仅在调用构造函数时创建。您应该将对象创建移动到 main()
函数中。
public static void main(String[] args){
People person1 = new People("Bob", 41);
People person2 = new People("Arthur", 32);
People person3 = new People("Tom",18);
for(People p : people) {
System.out.println(p.name);
}
}
您可以阅读有关静态变量的更多信息here
试试这个:
import java.util.ArrayList;
public class People {
int age;
String name;
People(String name, int age){
this.name = name;
this.age = age;
Main.people.add(this);
}
}
public class Main {
public static ArrayList<People> people = new ArrayList<People>();
public static void main(String[] args){
People person1 = new People("Bob", 41);
People person2 = new People("Arthur", 32);
People person3 = new People("Tom",18);
System.out.println(people.size());
for(People p : people) {
System.out.println(p.name);
}
}
}
您的代码稍微过于复杂了。要玩 ArrayList
,您可以在 main 方法中声明 ArrayList,它本身会创建 class People 的对象并在 ArrayList 中添加人物对象。您在 People class 中创建 People class 的三个实例的方式,创建 People 对象将导致循环引用,从而导致 WhosebugError
.
您应该将您的代码修改成这样,并注意为 People 对象打印有意义的信息,您需要重写 Object class 的 toString 方法,否则它只会打印可能出现的对象地址垃圾给你。
public class People {
int age;
String name;
public People(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return String.format("age: %s, name: %s", age, name);
}
public static void main(String[] args) {
ArrayList<People> people = new ArrayList<People>();
People person1 = new People("Bob", 41);
People person2 = new People("Arthur", 32);
People person3 = new People("Tom", 18);
people.add(person1);
people.add(person2);
people.add(person3);
people.forEach(System.out::println);
}
}
试试这个,如果遇到任何问题请告诉我。
所以我在查看 ArrayLists 时对如何向数组添加元素感到困惑,而没有为每个对象一遍又一遍地明确说明 people.add(person1)。我目前编译的这段代码没有在控制台中打印任何内容。我认为我的错误与对象构造函数和 people.add(this)
有关。我做错了什么?
import java.util.ArrayList;
public class People {
int age;
String name;
static ArrayList<People> people = new ArrayList<People>();
public People(String name, int age){
this.name = name;
this.age = age;
people.add(this);
}
People person1 = new People("Bob", 41);
People person2 = new People("Arthur", 32);
People person3 = new People("Tom",18);
public static void main(String[] args){
for(People p : people) {
System.out.println(p.name);
}
}
}
数组列表是一个 static
变量。您正在创建的 People
对象是非静态变量,仅在调用构造函数时创建。您应该将对象创建移动到 main()
函数中。
public static void main(String[] args){
People person1 = new People("Bob", 41);
People person2 = new People("Arthur", 32);
People person3 = new People("Tom",18);
for(People p : people) {
System.out.println(p.name);
}
}
您可以阅读有关静态变量的更多信息here
试试这个:
import java.util.ArrayList;
public class People {
int age;
String name;
People(String name, int age){
this.name = name;
this.age = age;
Main.people.add(this);
}
}
public class Main {
public static ArrayList<People> people = new ArrayList<People>();
public static void main(String[] args){
People person1 = new People("Bob", 41);
People person2 = new People("Arthur", 32);
People person3 = new People("Tom",18);
System.out.println(people.size());
for(People p : people) {
System.out.println(p.name);
}
}
}
您的代码稍微过于复杂了。要玩 ArrayList
,您可以在 main 方法中声明 ArrayList,它本身会创建 class People 的对象并在 ArrayList 中添加人物对象。您在 People class 中创建 People class 的三个实例的方式,创建 People 对象将导致循环引用,从而导致 WhosebugError
.
您应该将您的代码修改成这样,并注意为 People 对象打印有意义的信息,您需要重写 Object class 的 toString 方法,否则它只会打印可能出现的对象地址垃圾给你。
public class People {
int age;
String name;
public People(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return String.format("age: %s, name: %s", age, name);
}
public static void main(String[] args) {
ArrayList<People> people = new ArrayList<People>();
People person1 = new People("Bob", 41);
People person2 = new People("Arthur", 32);
People person3 = new People("Tom", 18);
people.add(person1);
people.add(person2);
people.add(person3);
people.forEach(System.out::println);
}
}
试试这个,如果遇到任何问题请告诉我。