public class CHECK {
public CHECK(){
String []wrkrs = {"Денис", "Саша", "Наталья", "Анатолий", "Юра", "Коля", "Катя", "Дима", "Антон","Тамара"};
int [] wrkrsPhone = {22626,22627,22628,22629,22630,22631,22632,22633,22634,22635};
String a = JOptionPane.showInputDialog(null, "Hello,friend!Do you wanna know, is that guy at work?Enter name:");
if(Arrays.asList(wrkrs).contains(a)){
JOptionPane.showMessageDialog(null, "That guy is at work!");
JOptionPane.showMessageDialog(null, "calling " + wrkrsPhone[wrkrsPhone.toString().indexOf(a)]);
}else{
JOptionPane.showMessageDialog(null, "Такого сотрудника нет!");
}
}
我有两个数组,包含整数和字符串。如您所见,我想将字符串数组中的元素数(例如,wrkrs number 3)添加到int数组中,称为wrkrs phone。我该怎么做?我尝试过IndexOf,但它没有用。
输出,我想要的是:
Enter name:
Юра
That guy is at work!
Calling Юра + wrkrsPhone(Юра).
答案 0 :(得分:5)
更好的解决方案是拥有一个包含工作人员姓名和电话号码的工人类。
然后,您可以使用HashMap<String,Worker>
代替数组来存储数据。
这使搜索更有效:
Map<String,Worker> workersMap = new HashMap<>();
workersMap.put ("Денис", new Worker ("Денис", 22626));
...
Worker worker = workersMap.get(a);
if (worker != null) {
call (worker.getPhone()); // or do whatever you want to do with the phone number
}
这比Arrays.asList(wrkrs).contains(a)
更有效,后者在列表上执行线性搜索。
答案 1 :(得分:-1)
...
List list = Arrays.asList(wrkrs);
if(list.contains(a)){
JOptionPane.showMessageDialog(null, "That guy is at work!");
JOptionPane.showMessageDialog(null, "calling " + wrkrsPhone[list.indexOf(a)]);
}
...
您最好使用Map或提取具有Contact
和name
属性的类phone
,但我认为这就是您所寻求的:)