Java 如何加载带有我创建的对象实例的ArrayList
注意:我编辑了我的代码,以了解人们是如何试图告诉我的,但它仍然没有给出我想要的输出。现在我的输出是“examples.search”。Person@55acc1c2“然而,我多次输入新的名字和姓氏。至少它在没有崩溃的情况下通过了代码 我正在学习如何使用ArrayList,并且需要加载包含我创建的对象实例的数组列表。我知道如何使用数组执行此操作,但对于此任务,我需要使用ArrayList执行此操作。这是我需要做的一个例子Java 如何加载带有我创建的对象实例的ArrayList,java,oop,arraylist,Java,Oop,Arraylist,注意:我编辑了我的代码,以了解人们是如何试图告诉我的,但它仍然没有给出我想要的输出。现在我的输出是“examples.search”。Person@55acc1c2“然而,我多次输入新的名字和姓氏。至少它在没有崩溃的情况下通过了代码 我正在学习如何使用ArrayList,并且需要加载包含我创建的对象实例的数组列表。我知道如何使用数组执行此操作,但对于此任务,我需要使用ArrayList执行此操作。这是我需要做的一个例子 // my "main" class package examples.se
// my "main" class
package examples.search;
import java.util.ArrayList;
import dmit104.Util;
public class MyPeople {
public static void main(String[] args) {
ArrayList<Person> people = new ArrayList<Person>();
Person tempPerson = new Person();
String firstName;
String lastName;
char choice = 'y';
int count = 1;
// fill my ArrayList
do {
people.add(tempPerson);
// I have a Util class that has a prompt method in it
firstName = Util.prompt("Enter First Name: ");
lastName = Util.prompt("Enter Last Name: ");
tempPerson.setFirstName(firstName);
tempPerson.setLastName(lastName);
count++;
choice = Util.prompt(
"Enter another person? [y or n]: ")
.toLowerCase().charAt(0);
} while (choice == 'y');
// display my list of people
for(int i = 0; i < people.size(); i += 1) {
System.out.print(people.get(i));
}
}
}
// my Person class which I am trying to build from
public class Person {
// instance variables
private String firstName;
private String lastName;
// default constructor
public Person() {
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
//我的“主”类
软件包示例搜索;
导入java.util.ArrayList;
导入dmit104.Util;
公共阶层的人{
公共静态void main(字符串[]args){
ArrayList people=新建ArrayList();
Person tempPerson=新的Person();
字符串名;
字符串lastName;
字符选择='y';
整数计数=1;
//填写我的数组列表
做{
people.add(temperson);
//我有一个Util类,其中有一个prompt方法
firstName=Util.prompt(“输入名字:”);
lastName=Util.prompt(“输入姓氏:”);
temperson.setFirstName(firstName);
temperson.setLastName(lastName);
计数++;
choice=Util.prompt(
“输入另一个人?[y或n]:”)
.toLowerCase().charAt(0);
}while(choice='y');
//显示我的人员列表
for(int i=0;i
我尝试过很多种方法,但无论我的ArrayList如何都无法填满。就像我提到的,我可以用一个数组或者即使我有一个已加载的构造函数方法来实现它,但我没有。在我的实际作业中,我应该用设定的方法来完成
我到处都找遍了,找不到解决问题的办法,因为星期五我的老师不在
事先非常感谢
Leo你必须创建一个
人物,然后将其添加到数组列表中
for (int i = 0; i < count; i++) {
Person person = new Person();
person.setFirstName("Foo");
person.setLastName("Bar");
people.add(person);
}
for(int i=0;i
您必须创建一个人员
,然后将其添加到数组列表
for (int i = 0; i < count; i++) {
Person person = new Person();
person.setFirstName("Foo");
person.setLastName("Bar");
people.add(person);
}
for(int i=0;i它崩溃了,因为您的线路people.get(i).setFirstName(firstName)
首先尝试在索引i中设置什么,但您尚未设置任何内容
首先将people[i]设置为空Person,或者使用firstName和lastName创建Person,然后使用people.add(Person)将其添加到people
它崩溃了,因为您的行people.get(i).setFirstName(firstName)
首先尝试在索引i中设置什么,但您尚未设置任何内容
首先将people[i]设置为空Person,或者使用firstName和lastName创建Person,然后使用people.add(Person)将其添加到people
您有一个ArrayList
,但仅此一项就只能定义一个潜在的人员
实例列表。但到目前为止,每个列表条目都是null
。get(i)
返回null
,下面的null.setFirstName(..)
导致NullPointerException
因此,您需要创建应该进入列表的Person
实例:
firstName = Util.prompt("Enter First Name: ");
Person p = new Person(); //create the instance
people.add(p); //add the instance to the list
p.setFirstName("..."); //set any values
您有一个ArrayList
,但仅此一项就只定义了一个潜在的Person
实例列表。但到目前为止,每个列表条目都是null
。get(i)
返回null
,下面的null.setFirstName(..)
导致NullPointerException
因此,您需要创建应该进入列表的Person
实例:
firstName = Util.prompt("Enter First Name: ");
Person p = new Person(); //create the instance
people.add(p); //add the instance to the list
p.setFirstName("..."); //set any values
现在将Person对象存储到ArrayList中并打印该对象。
要在打印Person对象时打印firstname和lastName,必须重写toString方法。
在Person类中添加以下代码
public String toString(){
return String.format("[Personn: firstName:%s ,lastName: %s]", firstName,lastName);
}
现在将Person对象存储到ArrayList中并打印该对象。
要在打印Person对象时打印firstname和lastName,必须重写toString方法。
在Person类中添加以下代码
public String toString(){
return String.format("[Personn: firstName:%s ,lastName: %s]", firstName,lastName);
}
至于第二个问题,您必须重写Person
类中的toString()
方法。您获得的输出,例如examples.search。Person@55acc1c2
是对象
类的默认toString()
方法,定义为class@hashCode
至于你的第二个问题,你必须重写Person
类中的toString()
方法。您获得的输出,例如examples.search。Person@55acc1c2
是对象
类的默认toString()
方法,定义为class@hashCode
您的“默认构造函数”根本不是构造函数。类的构造函数的名称必须与类的名称相同。在本例中,c