英文:
How do show the 5 objects in my ArrayList?
问题
目标 - 编写一个名为PhoneBookEntry的类,该类具有存储人名和电话号码的字段。该类应包含一个构造函数以及适当的访问器和修改器方法。然后编写一个程序,创建至少五个PhoneBookEntry对象,并将它们存储在一个ArrayList中。使用循环来显示ArrayList中每个对象的内容。
代码 -
package phonebook;
import java.util.ArrayList;
public class PhoneBookEntry {
private String personName;
private int phoneNumber;
public PhoneBookEntry(String personName, int phoneNumber) {
this.personName = personName;
this.phoneNumber = phoneNumber;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public int getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "Name: " + personName + ", Phone Number: " + phoneNumber;
}
public static void main(String[] args) {
ArrayList<PhoneBookEntry> phoneBook = new ArrayList<PhoneBookEntry>();
PhoneBookEntry Jack = new PhoneBookEntry("Jack Daniel", 6784395823L);
PhoneBookEntry Dave = new PhoneBookEntry("Dave Daniel", 3662631363L);
PhoneBookEntry John = new PhoneBookEntry("John Daniel", 4046428642L);
PhoneBookEntry Judy = new PhoneBookEntry("Judy Daniel", 2329529142L);
PhoneBookEntry Jennifer = new PhoneBookEntry("Jennifer Daniel", 5631248246L);
phoneBook.add(Jack);
phoneBook.add(Dave);
phoneBook.add(John);
phoneBook.add(Judy);
phoneBook.add(Jennifer);
for (int i = 0; i < phoneBook.size(); i++) {
System.out.println(phoneBook.get(i));
}
}
}
问题 - 该程序没有阻止其运行,但是它没有通过循环显示5个对象的内容。
英文:
Goal - Write a class named PhoneBookEntry that has fields for a person's name and phone number. The class should have a constructor and appropriate accessor and mutator methods. Then write a program that created at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList.
Code -
package phonebook;
import java.util.ArrayList;
public class PhoneBookEntry {
public PhoneBookEntry(String personName, int phoneNumber) {
}
public static void main(String[] args) {
ArrayList<PhoneBookEntry> phoneBook = new ArrayList<PhoneBookEntry>();
PhoneBookEntry Jack = new PhoneBookEntry("Jack Daniel", 678-439-5823);
PhoneBookEntry Dave = new PhoneBookEntry("Dave Daniel", 366-263-1363);
PhoneBookEntry John = new PhoneBookEntry("John Daniel", 404-642-8642);
PhoneBookEntry Judy = new PhoneBookEntry("Judy Daniel", 232-952-9142);
PhoneBookEntry Jennifer = new PhoneBookEntry("Jennifer Daniel", 563-124-8246);
for (int i = 0; i < phoneBook.size(); i++) {
System.out.println(phoneBook.get(i));
}
}
}
Problem - The program has no issue that prevents it from producing, however, it's not showing the 5 objects through a loop.
答案1
得分: 2
你没有将你的对象添加到你的ArrayList中。
使用:phoneBook.add(对象)
public static void main(String[] args) {
ArrayList<PhoneBookEntry> phoneBook = new ArrayList<PhoneBookEntry>();
phoneBook.add(new PhoneBookEntry("Jack Daniel", 678-439-5823));
//等等...
for (int i = 0; i < phoneBook.size(); i++) {
System.out.println(phoneBook.get(i));
}
}
英文:
You are not adding your objects to your ArrayList.
Use: phoneBook.add(OBJECT)
public static void main(String[] args) {
ArrayList<PhoneBookEntry> phoneBook = new ArrayList<PhoneBookEntry>();
phoneBook.add(new PhoneBookEntry("Jack Daniel", 678-439-5823));
//etc..
for (int i = 0; i < phoneBook.size(); i++) {
System.out.println(phoneBook.get(i));
}
}
答案2
得分: 0
你需要将所有元素添加到 ArrayList
中。为了简化操作,你可以使用 Arrays.asList
。
ArrayList<PhoneBookEntry> phoneBook = new ArrayList<PhoneBookEntry>(
Arrays.asList(new PhoneBookEntry("Jack Daniel", 678-439-5823),
new PhoneBookEntry("Dave Daniel", 366-263-1363),
new PhoneBookEntry("John Daniel", 404-642-8642),
new PhoneBookEntry("Judy Daniel", 232-952-9142),
new PhoneBookEntry("Jennifer Daniel", 563-124-8246)
)
);
如果你想保留变量名:
ArrayList<PhoneBookEntry> phoneBook = new ArrayList<PhoneBookEntry>(
Arrays.asList(Jack, Dave, John, Judy, Jennifer)
);
英文:
You need to add all of the elements to the ArrayList
. To simplify this, you can use Arrays.asList
.
ArrayList<PhoneBookEntry> phoneBook = new ArrayList<PhoneBookEntry>(
Arrays.asList(new PhoneBookEntry("Jack Daniel", 678-439-5823),
new PhoneBookEntry("Dave Daniel", 366-263-1363),
new PhoneBookEntry("John Daniel", 404-642-8642),
new PhoneBookEntry("Judy Daniel", 232-952-9142),
new PhoneBookEntry("Jennifer Daniel", 563-124-8246)
)
);
If you want to keep the variable names:
ArrayList<PhoneBookEntry> phoneBook = new ArrayList<PhoneBookEntry>(
Arrays.asList(Jack, Dave, John, Judy, Jennifer)
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论