2016-02-14 58 views
0
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 3號)的int數組,稱爲wrkrs電話。我該怎麼做?我試過的IndexOf,但它不工作。Java中的Int和String數組

輸出,我想是這樣的:

Enter name: 
Юра 
That guy is at work! 
Calling Юра + wrkrsPhone(Юра). 

回答

-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)]); 
} 
... 

ü更好地使用地圖或提取類Contact具有namephone財產,但我認爲這是什麼ü尋找:)

+0

謝謝!我來試試!) –

+0

Yeahh!這實際上是什麼,我需要) –

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),其執行清單上的線性搜索更有效。

+0

謝謝!我還沒有和MAP的工作,但我會很快做到這一點) –

相關問題