在Java中迭代数组并比较两个结果

时间:2022-11-13 20:20:43

Given and Array of Strings, such as:

给定和字符串数组,例如:

userMail usermail = new userMail();
List<String> data = new ArrayList<String>();

Where the Object userMail is defined as such:

Object 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;
    }

So here is where I am lost: Given an email address from a client, iterate through an array and find it. If it exists, then return some data that is associated with this email address and use REST to post it back to the client.

所以这就是我迷失的地方:给定客户端的电子邮件地址,遍历数组并找到它。如果存在,则返回与此电子邮件地址关联的一些数据,并使用REST将其发回客户端。

Even a high-level concept would be much appreciated. Thanks

即使是高级别的概念也会受到高度赞赏。谢谢

2 个解决方案

#1


0  

Its better to use HashMap or any Map construct which will clear the hastle of setting up a search algorithm.

最好使用HashMap或任何Map构造,这将清除设置搜索算法的麻烦。

For example

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

in case the key is not found null is returned. you can check the value returned against null to proceed further.

如果未找到密钥,则返回null。您可以检查返回的值为null以继续进行。

then return some data that is associated with this email address

然后返回与此电子邮件地址关联的一些数据

you can even store this data you want to return as the value store in the HashMap for the key.

您甚至可以将要返回的数据存储为密钥的HashMap中的值存储。

#2


-1  

Try this:

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
}

You can edit the if statement to compare whatever variable of the email you chose.

您可以编辑if语句以比较您选择的电子邮件的任何变量。

#1


0  

Its better to use HashMap or any Map construct which will clear the hastle of setting up a search algorithm.

最好使用HashMap或任何Map构造,这将清除设置搜索算法的麻烦。

For example

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

in case the key is not found null is returned. you can check the value returned against null to proceed further.

如果未找到密钥,则返回null。您可以检查返回的值为null以继续进行。

then return some data that is associated with this email address

然后返回与此电子邮件地址关联的一些数据

you can even store this data you want to return as the value store in the HashMap for the key.

您甚至可以将要返回的数据存储为密钥的HashMap中的值存储。

#2


-1  

Try this:

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
}

You can edit the if statement to compare whatever variable of the email you chose.

您可以编辑if语句以比较您选择的电子邮件的任何变量。