简体   繁体   中英

phonebook with arraylist in java

I want to try a simple implementation of phonebook with arraylist in java. First I made a class contains what the info. needed and second I want have another class which have methods like getting info and printing them.

and because I want to use array list this is what I've done so far, but the 'print' method keep giving me the error in for loop, is there any one who can help me to optimize my code and why I have this error.

this is the first class :

public class PhoneBook {
long number;
String name;
.
.
.
.

getter() and setter();
}

The second class with methods:

public class PhoneBookMethods {

ArrayList<PhoneBook> phoneBooks = new ArrayList<PhoneBook>();

public void getInfo(PhoneBook phoneBooks)
{

.
.
.
}
public void print(PhoneBook phoneBooks)
{
    for (PhoneBook p: phoneBooks) {// this is where I got the error
//foreach not applicable to type 'PhoneBook'
        System.out.print(p.getName());
        ....
    }
}

}

In your for-each loop, change

for (PhoneBook p: phoneBooks)

to

for (PhoneBook p: this.phoneBooks)

so that you would be accessing the phoneBooks arraylist, not the argument of the print method.

EDIT:

You can use the " this " keyword to make your code much more "explicit". For the example, in your case you have an argument called phoneBooks that has the same name as your ArrayList (member variable). So to explicitly differentiate between the two of them, use this.phonebooks to access the member variable phoneBooks (the ArrayList), and use phoneBooks to refer to the argument.

If you want to use instance variable phoneBooks then no need to pass any param in the method print() .

public void print()
{
    for (PhoneBook p: phoneBooks) {// this is where I got the error
//foreach not applicable to type 'PhoneBook'
        System.out.print(p.getName());
        ....
    }
}

OR if you really want to pass param rename the param name

public void print(ArrayList<PhoneBook> phoneBookList)
{
    for (PhoneBook p: phoneBookList) {// this is where I got the error
//foreach not applicable to type 'PhoneBook'
        System.out.print(p.getName());
        ....
    }
}

public void print(PhoneBook phoneBooks)

Your parameter phoneBooks masks the field (the array) also named phoneBooks. So compiler tries to treat parameter as list and failes.

Actually at first you have some design issues. The way you think what is a PhoneBook is invalid. You should consider a phonebook something holds several phones on it. Therefore, you may have a phone class like below:

public class Phone {

    private String number;
    private String name;

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

And a phonebook class responsible for holding those phone objects:

public class PhoneBook extends ArrayList<Phone> {

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();

        for (Phone phone : this) {
            stringBuilder.append("----------------------------\n");
            stringBuilder.append("Name:" + phone.getName() + "\n");
            stringBuilder.append("Number:" + phone.getNumber() + "\n");
        }

        return stringBuilder.toString();
    }

}

It is a arraylist of Phone, nothing more. Thus, you can add or remove a phone directly via phone book. This is how to use it:

public class MAIN {

    public static void main(String[] args) {
        Phone myPhone = new Phone();
        myPhone.setName("Eray");
        myPhone.setNumber("0533XXXXXXX");

        Phone girlfriendPhone = new Phone();
        girlfriendPhone.setName("Canan");
        girlfriendPhone.setNumber("0544XXXXXXX");

        Phone yourPhone = new Phone();
        yourPhone.setName("Bita Mirshafiee");
        yourPhone.setNumber("0599XXXXXXX");


        PhoneBook phoneBook = new PhoneBook();
        phoneBook.add(myPhone);
        phoneBook.add(girlfriendPhone);
        phoneBook.add(yourPhone);

        System.out.println(phoneBook);
    }

}

Finally, this is the output:

----------------------------
Name:Eray
Number:0533XXXXXXX
----------------------------
Name:Canan
Number:0544XXXXXXX
----------------------------
Name:Bita Mirshafiee
Number:0599XXXXXXX

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM