1
我正在處理項目,它將一些人名和電話號碼存儲在並行數組中。我已經明白了。我只需要一些關於如何進行搜索的想法。例如,用戶應該能夠輸入名稱或名稱的一部分,並且它將輸出具有相應電話號碼的該名稱或名稱。這是我到目前爲止:搜索並行字符串Java
public class PeoplesNumbers {
private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> phoneNumber = new ArrayList<String>();
//will allow to add names in main program
public void plusName(String names, String phoneNumbers) {
name.add(names);
phoneNumber.add(phoneNumbers);
}
/**
* Good way to test program before making search method.
* This will just test output of names in the main class.
*/
public void output() {
for (int x = 0; x < name.size(); x++) {
System.out.println(name.get(x) + ": ");
System.out.println("\t" + phoneNumber.get(x));
System.out.println();
}
}
}
public class Telemarketing {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//instance of PeopleNumbers Class
PeoplesNumbers g1 = new PeoplesNumbers();
g1.plusName("Harrision,Rose", "555-2234");
g1.plusName("James, Jean", "555-9098");
g1.plusName("Smith, William","555-1785");
g1.plusName("Smith, Brad", "555-9224");
//This below was used to test program out before I made the search method
g1.output();
}
}
你有什麼建議?
不要使用兩個列表:使用'Map'。 –
你想讓它成爲名字的一部分嗎?即'arr'會給'Harrison,Rose' –
如果這個要求是使用並行數組的話,那還是可以的嗎? – ztmcoder