我遇到了存储成员对象的arrayList问题。我认为这可能与我宣布它的方式有关。这就是我到目前为止所拥有的
会员类
package assignment;
import java.util.ArrayList;
public class Member {
private int memberID;
private String memberName;
private int memberAge;
private int numOfBooksLoaned;
private int penalties;
public Member(int memberID, String memberName, int memberAge, int numOfBooksLoaned, int penalties) {
this.memberID = memberID;
this.memberName = memberName;
this.memberAge = memberAge;
this.numOfBooksLoaned = numOfBooksLoaned;
this.penalties = penalties;
}
public int getMemberID() {
return memberID;
}
public void setMemberID(int memberID) {
this.memberID = memberID;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
public int getMemberAge() {
return memberAge;
}
public void setMemberAge(int memberAge) {
this.memberAge = memberAge;
}
public int getNumOfBooksLoaned() {
return numOfBooksLoaned;
}
public void setNumOfBooksLoaned(int numOfBooksLoaned) {
this.numOfBooksLoaned = numOfBooksLoaned;
}
public int getPenalties() {
return penalties;
}
public void setPenalties(int penalties) {
this.penalties = penalties;
}
}
MemberList类
package assignment;
import java.util.Scanner;
import java.util.ArrayList;
public class MemberList {
private ArrayList<Member> Members;
public MemberList(ArrayList<Member> Members)
{
this.Members = Members;
}
public void addNewMember()
{
Scanner input = new Scanner(System.in);
boolean successful = false;
int memberID;
String memberName;
int memberAge;
do {
System.out.println("/t/tCreate new member" + "/nPlease enter your full name: ");
memberName = input.nextLine();
for (int i = 0; i < Members.size(); i++) {
if (Members.get(i).getMemberName().equalsIgnoreCase(memberName)) {
System.out.println("This member name is already in use");
} else {
successful = true;
}
}
} while (successful == false);
问题出在if语句
中的for循环之后if (Members.get(i).getMemberName().equalsIgnoreCase(memberName))
错误表明类型getMemberName()
未定义Member
。
有什么想法吗?
我使用了完全相同的使用arrayList的相同方式,它工作正常,但由于某种原因它现在不起作用。
答案 0 :(得分:-1)
尝试以这种方式使用foreach循环
for (Member m : Members) {
if (m.getMemberName().equalsIgnoreCase(memberName)) {
System.out.println("This member name is already in use");
} else {
successful = true;
}
}
另一件事是,不鼓励在Java中的类成员的第一个字母处使用大写字母,将列表定义为
private ArrayList<Member> members;