如何显示我ArrayList中的5个对象?

huangapple go评论76阅读模式
英文:

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&lt;PhoneBookEntry&gt; phoneBook = new ArrayList&lt;PhoneBookEntry&gt;();
PhoneBookEntry Jack = new PhoneBookEntry(&quot;Jack Daniel&quot;, 678-439-5823);
PhoneBookEntry Dave = new PhoneBookEntry(&quot;Dave Daniel&quot;, 366-263-1363);
PhoneBookEntry John = new PhoneBookEntry(&quot;John Daniel&quot;, 404-642-8642);
PhoneBookEntry Judy = new PhoneBookEntry(&quot;Judy Daniel&quot;, 232-952-9142);
PhoneBookEntry Jennifer = new PhoneBookEntry(&quot;Jennifer Daniel&quot;, 563-124-8246);
for (int i = 0; i &lt; 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&lt;PhoneBookEntry&gt; phoneBook = new ArrayList&lt;PhoneBookEntry&gt;();

    phoneBook.add(new PhoneBookEntry(&quot;Jack Daniel&quot;, 678-439-5823));
    //etc..
        
    for (int i = 0; i &lt; 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&lt;PhoneBookEntry&gt; phoneBook = new ArrayList&lt;PhoneBookEntry&gt;(
Arrays.asList(new PhoneBookEntry(&quot;Jack Daniel&quot;, 678-439-5823),
new PhoneBookEntry(&quot;Dave Daniel&quot;, 366-263-1363),
new PhoneBookEntry(&quot;John Daniel&quot;, 404-642-8642),
new PhoneBookEntry(&quot;Judy Daniel&quot;, 232-952-9142),
new PhoneBookEntry(&quot;Jennifer Daniel&quot;, 563-124-8246)
)
);  

If you want to keep the variable names:

ArrayList&lt;PhoneBookEntry&gt; phoneBook = new ArrayList&lt;PhoneBookEntry&gt;(
Arrays.asList(Jack, Dave, John, Judy, Jennifer)
);

huangapple
  • 本文由 发表于 2020年8月15日 02:54:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63418580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定