Arraylist麻烦,包不存在编译错误

时间:2014-12-13 02:14:38

标签: java arraylist

当我编译此代码时,我得到“包m1和m2不存在”,任何帮助将不胜感激,我在与Arraylists合作时是全新的。我知道它非常接近所以任何帮助它编译和输出将非常感谢!

2 errors found:
File: /Users/gcaruso/Documents/Member.java  [line: 66]
Error: /Users/gcaruso/Documents/Member.java:66: package m1 does not exist
File: /Users/gcaruso/Documents/Member.java  [line: 72]
Error: /Users/gcaruso/Documents/Member.java:72: package m2 does not exist



import java.util.*;


public class Member {

    private int id;
    private String name;
    ArrayList<String> books = new ArrayList<String>();


    public Member() {
        id = 0;
        name = "John Doe";
    }

    public Member(int pId, String pName) {
        id = pId;
        name = pName;
    }

    public int getId() {
        return id;
    }

    public void setId(int pId) {
        id = pId;
    }

    public String getName() {
        return name;
    }

    public void setName(String pName) {
        name = pName;
    }

    public ArrayList<String> getBooks() {
        return books;
    }

    public void setBooks(ArrayList<String> pBooks) {
        books = pBooks;
    }

    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append("ID: " + getId() + "  Member: " + getName() + "\n");
        buf.append("Book: " + books + "\n");
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        String[] list = {"Java Data Structures", "The Bible", "Grapes of Wrath",
                "Goldfinger", "Sam I Am", "The Cat in the Hat", "Shawshenk Redemption",
                "Green Eggs and Ham", "Linus and Lucy", "Abraham Lincoln"};

        Member m1 = new Member();
        m1.setId(431);
        m1.setName("William Wallace");
        for (int i = 0; i < 5; i++) {
            m1.ArrayList<String> books = list[i];
        }

        Member m2 = new Member(7010, "Bonny Clyde");
        for (int i = 5; i < list.length; i++) {
            int x = i - 5;
            m2.ArrayList<String> books = list[i];
        }
        System.out.println(m1.toString());
        System.out.println();
        System.out.println(m2.toString());
    }

}

1 个答案:

答案 0 :(得分:3)

m1.ArrayList<String> books = list[i]

将其替换为m1.books.add(list[i]),将m2

替换为{{1}}