我将一个字符串传递给一个以逗号作为分隔符的方法。
"TMJ,Emma,Sarah"
我使用“,”作为要拆分的正则表达式来标记这个字符串。
然后,我迭代标记化数组的长度,将每个元素与所有可能值的 HashMap 进行比较。如果正在测试的值是 HashMap 中的一个键,那么我将获取该键的值并将其存储在另一个字符串中。
我想将键的每个值附加到保存值的字符串中。
它似乎只迭代一次,然后跳出循环并仅返回它在哈希图中找到的第一件事。
谁能解释为什么?在此先感谢马特。
public static String getrecipientIntergerValues(String recipient) {
Log.e(TAG, "recipient string list passed in to app obj = " + recipient);
String[] tokenizedRecipient = recipient.split(",");
String recipientAsInteger = "";
for(int i = 0; i < tokenizedRecipient.length; i++){
Log.e(TAG, "tokenizedRecipient = " + tokenizedRecipient[i].toString());
}
Log.e(TAG, "tokenizedRecipient length = " + tokenizedRecipient.length);
for(int i = 0; i < tokenizedRecipient.length; i++){
if(recipients.containsKey(tokenizedRecipient[i].toString())){
Log.e(TAG, "hashmap contains key " + tokenizedRecipient[i].toString() + "with value " + recipients.get(tokenizedRecipient[i].toString()));
String integerValueOfName = recipients.get(tokenizedRecipient[i].toString());
recipientAsInteger = recipientAsInteger + integerValueOfName + ",";
}
}
Log.e(TAG, "recipient list as integers = " + recipientAsInteger);
return recipientAsInteger;
}
.
09-20 16:33:51.039: E/NfcScannerApplication(25835): recipient string list passed in to app obj = Emma, TMJ,
09-20 16:33:51.039: E/NfcScannerApplication(25835): tokenizedRecipient = Emma
09-20 16:33:51.064: E/NfcScannerApplication(25835): tokenizedRecipient = TMJ
09-20 16:33:51.064: E/NfcScannerApplication(25835): tokenizedRecipient =
09-20 16:33:51.079: E/NfcScannerApplication(25835): tokenizedRecipient length = 3
09-20 16:33:51.079: E/NfcScannerApplication(25835): hashmap contains key Emmawith value 3
09-20 16:33:51.089: E/NfcScannerApplication(25835): recipient list as integers = 3,