我在这里开展项目,将一些人的名字和电话号码存储在一个并行数组中。我已经明白了。我只需要一些关于如何进行搜索的想法。例如,用户应该能够输入名称或名称的一部分,并且它将输出具有相应电话号码的名称或名称。以下是我到目前为止的情况:
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();
}
}
你有什么建议?
答案 0 :(得分:1)
这是你需要的逻辑: