【问题标题】:java phone book array how to add new information and display old infojava电话簿数组如何添加新信息和显示旧信息
【发布时间】:2019-11-25 23:00:32
【问题描述】:

您好,我想做一个电话簿,这是我目前得到的,任何帮助都会得到帮助。条目应按姓氏排序,每个联系人都必须按字母顺序排列。并且在每个新条目之后应该显示旧条目

package book;
import java.util.Scanner;

public class Book {
    public static void main(String[] args) {
       Contact[] contacts = new Contact[20];

       Scanner scanner = new Scanner(System.in);

         for (int i = 0 ; i < contacts.length; i++) {
         }
    }

类:

package book;

public class Contact {

    private String firstName;
    private String lastName;
    private String phone;
    private String email;

    public Contact(String firstName, String lastName, String phone, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.phone = phone;
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getPhone() {
        return phone;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }




}

【问题讨论】:

  • 这是家庭作业吗?
  • 您好!您能否添加一些关于您尝试按字母顺序排序的代码,或更具体的问题?查看stackoverflow.com/questions/34941422/… 可能有助于排序部分

标签: java


【解决方案1】:
package main.test;

public class Contact implements Comparable<Contact>{
    private String firstName;
    private String lastName;
    private String phone;
    private String email;

    public Contact(String firstName, String lastName, String phone, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.phone = phone;
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getPhone() {
        return phone;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public int compareTo(Contact obj) {
        // TODO Auto-generated method stub
        return this.lastName.compareTo(obj.lastName);

    }

    @Override
    public String toString() {
        return "Contact [firstName=" + firstName + ", lastName=" + lastName + ", phone=" + phone + ", email=" + email
                + "]";
    }


}

====主要 class==

public class MainContact {
        public static void main(String[] args) {
            List<Contact> lsCont = new ArrayList<Contact>();

            Scanner scanner = new Scanner(System.in);
            String firstName;
            String lastName;
            String phone;
            String email;
            for (int i = 0; i < 20; i++) {
                System.out.println("insert firstname : ");
                firstName = scanner.nextLine();
                System.out.println("insert lastName : ");
                lastName = scanner.nextLine();
                System.out.println("insert phone : ");
                phone = scanner.nextLine();
                System.out.println("insert email : ");
                email = scanner.nextLine();
                Contact cont = new Contact(firstName, lastName, phone, email);
                lsCont.add(cont);
                Collections.sort(lsCont);
                for (Contact contact : lsCont) {
                    System.out.println(contact.toString());
                }   
            }       

        }
    }

【讨论】:

  • 有什么办法不使用数组列表吗?
猜你喜欢
  • 2013-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多