//Example from class that I was trying to base off of:
public void printMajors(String major) {
for(Student x : dir) {
//dir = new HashMap<Integer, Student>(); and Student is the second class.
if (x.getMajor().equals(major)) {
System.out.println(x.getName());
}
}
}
我想打印出所有插入了相同区号的人,但迭代器在找到匹配后会中途停止。如何让它继续循环并打印出具有相同区号的每个人?
//代码中的问题
public boolean printAreaCode(String areacode) {
for (String p : phonebook.values()) {
if (p.startsWith(areacode)) {
for (String name : phonebook.keySet()) {
String key = name;
System.out.println(key);
return true;
}
}
}
return false;
}
整个作业
import java.util.HashMap;
import java.util.Iterator;
public class PhoneBook {
private HashMap<String, String> phonebook;
public static void main(String[] args) {
PhoneBook phone = new PhoneBook();
phone.addEntry("Alexander Schnell", "987-654-3210");
phone.addEntry("Bob the Builder", "555-555-5555");
phone.addEntry("Michael", "465-858-5555");
phone.addEntry("Robert", "778-555-1234");
phone.addEntry("Charlie", "987-546-4564");
phone.addEntry("Steve", "909-555-7845");
System.out.println("All of the people in the directory:");
phone.printListings(); //works
System.out.println("");
System.out.println("People with matching area codes:"); //WIP
phone.printAreaCode("987");
System.out.println("");
System.out.println("Prints phone number for requested person:");
phone.getNumber("Bob the Builder"); //works
}
//Adds a new entry to the phone book (naturally).
//It has two parameters, both Strings: the person’s name and their phone
//number (in the form “610-499-4035”).
public void addEntry(String name, String number) {
phonebook.put(name, number);
}
//prints the names and phone numbers of everyone in the phone book
private void printListings() {
for (Iterator<String> it = phonebook.keySet().iterator(); it.hasNext();) {
for (String p : phonebook.values()) {
String key = (String) it.next();
System.out.println(key + ": " + p.toString());
}
}
}
//looks up a number in the map.
//It has one parameter (a person’s name) and returns a PhoneNumber object.
public String getNumber(String name) {
//public void getNumber(String name) {
// no return statement
System.out.println(phonebook.get(name));
return phonebook.get(name);
}
//prints the names of all people with the given area code
public boolean printAreaCode(String areacode) {
for (String p : phonebook.values()) {
if (p.startsWith(areacode)) {
for (String name : phonebook.keySet()) {
String key = name;
System.out.println(key);
return true;
}
}
}
return false;
}
public PhoneBook() {
phonebook = new HashMap<String, String>();
}
}
我甚至认为这个班级与我的主班级沟通。 我的老师刚才说必须加入。 这可能是一个问题。
public class PhoneNumber {
private String areacode;
private String prefix;
private String four;
//Insert a phone number
public PhoneNumber(String number) {
String[] parts = number.split("-");
areacode = parts[0];
prefix = parts[1];
four = parts[2];
}
@Override
public String toString() {
String s = areacode + "-" + prefix + "-" + four;
return s;
}
}
答案 0 :(得分:1)
您正在迭代地图错误您的return true;
声明是导致问题的原因,但我们暂时会忽略它。
始终迭代这样的地图:
for(Map.Entry<String, String> entry: phoneBook.entrySet())
{
if(entry.getValue().startsWith(areaCode))
{
System.out.println(entry.getKey());
}
}
答案 1 :(得分:1)
因此,您需要打印key
value
startsWith
的{{1}},因此您的代码基本上有点落后。
目前您编码扫描寻找匹配的值,这没关系,但它会打印所有areaCode
值吗?
相反,您需要从每个key
开始,获取key
,然后进行测试,看看与该关键字相关联的value
是否与value
匹配,例如
areacode
现在,这非常低效。基本上,对于每次迭代,您要求public boolean printAreaCode(String areacode) {
boolean found = false;
for (String key : phonebook.keySet()) {
String value = phonebook.get(key);
if (value.startsWith(areacode)) {
System.out.println(key);
found = true;
}
}
return found;
}
找到与Map
相关联的值,这需要key
搜索其所有各种条目/节点,尝试找到匹配的密钥。虽然在你的例子中,它可能不是一个大的延迟,但它应该尽可能避免。
相反,Map
提供对Map
的访问权限,Entry
将key
和value
配对到一个对象中,使其访问速度更快,例如......
public boolean printAreaCode(String areacode) {
boolean found = false;
for (Map.Entry<String, String> entry : phonebook.entrySet()) {
String value = entry.getValue();
if (value.startsWith(areacode)) {
String key = entry.getKey();
System.out.println(key);
found = true;
}
}
return found;
}
现在,HashMap
是Map
的实现,所以通常我更喜欢使用
private Map<String, String> phonebook;
而不是
private HashMap<String, String> phonebook;
除非我特别需要HashMap
中定义的某些功能,否则,如果我稍后使用TreeMap
,则代码可能无关紧
答案 2 :(得分:0)
你可以添加这条线吗?! String
我很好奇为什么你不能添加import java.util.Map;
声明;是标准的一部分还是你不知道怎么做?
你必须两次迭代你的地图。是的,这是高度效率低下的代码。
import
}
答案 3 :(得分:0)
我相信您的意图是打印代码以特定值开头的电话号码。因此,我们不必从方法返回任何内容,您可以将返回类型设置为void。您可以使其与printListings
public void printAreaCode(String areacode) {
for (String p : phonebook.values()) {
if (p.startsWith(areacode)) {
for (String name : phonebook.keySet()) {
String key = name;
System.out.println(key);
}
}
}
}