使用List集合时,通常情况下声明为List类型,实例化时根据实际情况的需要,实例化为ArrayList;
List<String> l = new ArrayList<String>();// 利用ArrayList类实例化List集合
本项目Persons为实体类,PhoneBooks为实现类;
Persons.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Persons {
private String name;
private String sex;
private String phonenum;
private String age;
private String qq;
private String address;
Scanner sc = new Scanner(System.in);
public Persons addPerson() {
Persons person = new Persons();
System.out.println("输入姓名:");
String name = sc.next();
System.out.println("输入性别:");
String sex = sc.next();
System.out.println("输入电话号码:");
String phonenum = sc.next();
System.out.println("输入年龄:");
String age = sc.next();
System.out.println("输入qq:");
String qq = sc.next();
System.out.println("输入地址:");
String address = sc.next();
person.setName(name);
person.setAge(age);
person.setPhonenum(phonenum);
person.setQq(qq);
person.setSex(sex);
person.setAddress(address);
System.out.println(person);
System.out.println("添加成功");
return person;
}
public Persons selectByName(List<Persons> list){
Persons person = new Persons();
System.out.println("请输入姓名:");
String name = sc.next();
for(int j = 0;j<list.size();j++){
if(name.equals(list.get(j).getName())){
System.out.println(list.get(j));
person = list.get(j);
return person;
}else {
person = null;
}
}
return person;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "姓名=" + name + ", 性别=" + sex + ", 电话号码=" + phonenum + ", 年龄=" + age + ", qq=" + qq
+ ", 地址=" + address ;
}
}
PhoneBooks.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class phoneBooks {
public static void main(String[] args){
List<Persons> list = new ArrayList<Persons>();
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("----------------------电话簿系统------------"
+ "----------");
System.out.println("1.添加 2.删除 3.修改 4.查询所有 5.根据姓名查询 0.退出");
System.out.println("----------------------电话簿系统------------"
+ "----------");
System.out.println("请选择业务:");
Persons p1 = new Persons();
switch (sc.nextInt()){
case 1:
System.out.println("----------------------添加电话簿------------"
+ "----------");
list.add(p1.addPerson());
break;
case 2:
System.out.println("----------------------删除电话簿------------"
+ "----------");
p1 = p1.selectByName(list);
if(p1!=null){
System.out.println("确定么?1(是)0(否)");
int answer = sc.nextInt();
if(answer ==1){
list.remove(p1);
System.out.println("删除成功");
}
}else{
System.out.println("此人不存在");
}
break;
case 3:
System.out.println("----------------------修改电话簿------------"
+ "----------");
p1 = p1.selectByName(list);
if(p1!=null){
System.out.println("确定么?1(是)0(否)");
int answer = sc.nextInt();
if(answer ==1){
list.remove(p1);
list.add(p1.addPerson());
System.out.println("修改成功");
}
}else{
System.out.println("此人不存在");
}
break;
case 4:
System.out.println("----------------------查询所有------------"
+ "----------");
for(int j = 0;j<list.size();j++){
System.out.println(list.get(j));
}
break;
case 5:
System.out.println("----------------------根据姓名查询------------"
+ "----------");
p1 = p1.selectByName(list);
if(p1==null){
System.out.println("此人不存在");
}
break;
case 0:return;
}
}
}
}
来源:CSDN
作者:luxury.
链接:https://blog.csdn.net/qq_40743463/article/details/104597589