尝试打印出arraylist时出错

时间:2015-01-15 18:13:39

标签: java debugging

我正在恢复的错误是“此处不允许使用void类型”如何修复此错误以便打印出我的数组列表。

 import java.util.ArrayList;



public class Library
{


    private ArrayList<Member>listOfMembers;
    public Library()
    {
        listOfMembers = new ArrayList<Member>();
    }
    public void storeMember(Member Member)
    {
        listOfMembers.add(Member);
    }
    public int numberOfMembers()
    {
        return listOfMembers.size();
    }

    public void listMembers()
   {
   for (int item=0; item<listOfMembers.size(); item++ ) {
     Member m = listOfMembers.get (item);
    System.out.println(m.GetWholeName());
        }
    } 


}

这是我的会员班,以防你需要它

class Member
{
    // The fields.
    private String firstname;
    private String lastname;
    private Integer number;
    private Integer id;

    /**

     hehe
     */
    public Member(String firstName, String lastName, Integer telNumber, Integer memberId)
    {
        firstname = firstName;
        lastname = lastName;
        number = telNumber;
        id = memberId;

    }


    // Add the methods here ...
    public void GetWholeName(){

    System.out.println(firstname + (" ") + lastname);

}
}

我正在尝试使用库类中的数组列表打印成员类的名字和姓氏

2 个答案:

答案 0 :(得分:3)

您正在尝试打印出返回void的m.GetWholeName()的结果。更改GetWholeName以返回String,或者只需致电GetWholeName,因为它已经将名称打印到标准输出,尽管该行为与“{1}}完全匹配”这个名字暗示的行为。

public String GetWholeName() {

    return firstname + " " + lastname;

}

答案 1 :(得分:1)

在会员班中改变这个:

System.out.println(firstname + (" ") + lastname);

到此:

return firstname + " " + lastname;