利HashMap用类对象存储公司电话号码簿,其中包含公司的电话号码和公司 名称,然后进行删除一个公司和查询一个公司的操作。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class 电话号码 {
/*利HashMap用类对象存储公司电话号码簿,其中包含公司的电话号码和公司 名称,
* 然后进行删除一个公司和查询一个公司的操作。
*/
public static void main(String[] args) {
Map<String, Tel> map = new HashMap<String, Tel>();
Tel tel1 = new Tel("13888888888", "A公司");
Tel tel2 = new Tel("13999999999", "B公司");
Tel tel3 = new Tel("13777777777", "C公司");
Tel tel4 = new Tel("13666666666", "D公司");
map.put(tel1.company, tel1);
map.put(tel2.company, tel2);
map.put(tel3.company, tel3);
map.put(tel4.company, tel4);
Scanner in = new Scanner(System.in);
System.out.println("请输入要删除公司名称");
String name = in.next();
map.remove(name);
System.out.println("请输入要查询公司名称");
name = in.next();
System.out.println(map.get(name));
}
}
class Tel{
String mobile;
String company;
public Tel(String mobile,String compay) {
this.mobile = mobile;
this.company = company;
}
@Override
public String toString() {
return "电话号码:"+this.mobile + ",公司名称:" + this.company;
}
}
public class Test {
public static void main(String[] args) {
class Info {
String tel;// 电话
String company;// 公司
}
Info info1 = new Info();
info1.tel="123";
info1.company="腾讯";
//添加公司信息
Map<String,Info> map = new HashMap<String,Info>();
map.put("info1", info1);
//查询公司信息
Info inf = map.get("info1");
System.out.println(inf.tel+" "+inf.company);
//删除公司信息
map.remove("info1");
}
}
大概的代码,要看你的具体需求是什么
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class 电话号码 {
/*利HashMap用类对象存储公司电话号码簿,其中包含公司的电话号码和公司 名称,
* 然后进行删除一个公司和查询一个公司的操作。
*/
public static void main(String[] args) {
Map<String, Tel> map = new HashMap<String, Tel>();
Tel tel1 = new Tel("13888888888", "A公司");
Tel tel2 = new Tel("13999999999", "B公司");
Tel tel3 = new Tel("13777777777", "C公司");
Tel tel4 = new Tel("13666666666", "D公司");
map.put(tel1.company, tel1);
map.put(tel2.company, tel2);
map.put(tel3.company, tel3);
map.put(tel4.company, tel4);
Scanner in = new Scanner(System.in);
System.out.println("请输入要删除公司名称");
String name = in.next();
map.remove(name);
System.out.println("请输入要查询公司名称");
name = in.next();
System.out.println(map.get(name));
}
}
class Tel{
String mobile;
String company;
public Tel(String mobile,String company) {
this.mobile = mobile;
this.company = company;
}
@Override
public String toString() {
return "电话号码:"+this.mobile + ",公司名称:" + this.company;
}
}
public static void main(String[] args) {
HashMap<String, String> map=new HashMap<String, String>();
map.put("1361101111", "A公司");
map.put("2", "B公司");
map.put("13611011311", "C公司");
map.put("4", "D公司");
map.put("5", "E公司");
Scanner sc=new Scanner(System.in);
System.out.println("请输入想删除公司电话:");
String tel=sc.next();
//如果输入的电话存在
if(map.keySet().contains(tel)){
//通过电话删除公司
map.remove(tel);
}else{
System.out.println("该电话的公司不存在");
}
System.out.println("请输入想查询公司电话:");
tel=sc.next();
if(map.keySet().contains(tel)){
//通过电话删除公司
String name=map.get(tel);
System.out.println("公司电话:"+tel+",公司名称:"+name);
}else{
System.out.println("该电话的公司不存在");
}
}
以上为代码,万望采纳
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632