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