[英]iterate through array in Java and compare two results
給定和字符串數組,例如:
userMail usermail = new userMail();
List<String> data = new ArrayList<String>();
對象userMail的定義如下:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class userMail {
public userMail(){
}
public List<String> email = new ArrayList<String>();
public List<String> getEmail() {
return email;
}
public void setEmail(List<String> email) {
this.email = email;
}
所以這是我迷路的地方:給定客戶的電子郵件地址,遍歷數組並找到它。 如果存在,則返回與此電子郵件地址關聯的一些數據,並使用REST將其發布回客戶端。
即使是高級概念也將不勝感激。 謝謝
最好使用HashMap或任何Map構造,這將消除設置搜索算法的麻煩。
例如
public static void main(String[] args)
{
HashMap<String,String> xdr = new HashMap<String, String>();
xdr.put("bdx@atr.com", "Border line Statement");
System.out.println(xdr.get("pdf"));//output returned is null
String valx = xdr.get("bdx@atr.com");
String valy = xdr.get("pdf");
if(valy == null)
System.out.println("This is null value" + valy);
if(valx == null)
System.out.println(valx);
}
如果找不到密鑰,則返回null。 您可以檢查返回的值是否為null,以繼續進行操作。
然后返回與此電子郵件地址關聯的一些數據
您甚至可以將要返回的數據存儲為鍵的HashMap中的值存儲。
嘗試這個:
public email checkForEmail(testEmail)
{
for(int x = 0; x < emailList.length-1; emailList--) //searches through list of emails
{
if(emailList[x].equals(testEmail)) //if current index position equals email you search for
return emailList[x]; //return that email
}
return null; //returns null if the email address wasn't contained
}
您可以編輯if語句以比較所選電子郵件的任何變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.