如何在java中使用线性搜索?

里克·韦恩

如果我必须接受用户输入(即姓名),我有一组 [3][3] ID、姓名、城市,然后显示其他人的详细信息,如 id 和城市

public class Source {

    String customerDetails[][]=new String[5][3];

    Source() {
        customerDetails[0][0]="1001";
        customerDetails[0][1]="Nick";
        customerDetails[0][2]="Chicago";
    
        customerDetails[1][0]="1008";
        customerDetails[1][1]="James";
        customerDetails[1][0]="San Diego";
        
        customerDetails[2][0]="1002";
        customerDetails[2][1]="Tony";
        customerDetails[2][2]="New York";
        
        customerDetails[3][0]="1204";
        customerDetails[3][1]="Tom";
        customerDetails[3][2]="Houston";
        
        customerDetails[4][0]="1005";
        customerDetails[4][1]="Milly";
        customerDetails[4][2]="San Francisco";
    }

}

请建议我。我是java新手,谢谢!

施里杜特·科塔里

您需要遍历 2D 数组并需要匹配给定的用户名,数组中的每个名称,如果找到匹配项,您可以返回包含在自定义类中的用户详细信息

public List<Customer> findCustomersByName(String customerName) {
    int length = customerDetails.length;
    List<Customer> customersmatching = new ArrayList<>();

    for (int i = 0; i < length; i++) {
        if (customerName != null && customerName.equals(customerDetails[i][1])) {
            customersmatching.add(new Customer(customerDetails[i][0], customerDetails[i][1], customerDetails[i][2]));
        }
    }
    return customersmatching;
}

private static class Customer {
    String userId;
    String userName;
    String userCity;

    public Customer(String userId, String userName, String userCity) {
        this.userId = userId;
        this.userName = userName;
        this.userCity = userCity;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserCity() {
        return userCity;
    }

    public void setUserCity(String userCity) {
        this.userCity = userCity;
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在weasyprint中使用线性渐变?

如何在Java中使用Aether搜索RemoteRepositories?

如何在Java中使用哈希表进行搜索?

如何在Java中使用pdfbox 2.0创建线性化(快速Web视图)的pdf?

如何在TSConfig addToList中使用多行代替线性列表?

如何在TensorFlow中使用线性激活函数?

如何在Haskell中使用线性库创建矩阵

如何在Matlab中使用线性索引为向量赋值?

如何在Gnuplot中使用非线性轴?

如何在 GODOT 中使用线性插值

如何在Spotlight搜索中使用通配符?

如何在php中使用mysql搜索?

如何在Elasticsearch中使用搜索

如何在 Laravel 中使用搜索功能?

如何在 MySQL 的搜索中使用 @ 进行 LIKE 搜索?

如何在Firefox中使用Google搜索而不是OpenDNS搜索

如何在Java Swing中使用vlcj播放的视频中添加搜索栏?

如何在 Java 中使用 String 類型的 ArrayList 搜索 String 值?

如何在JAVA中使用XPATH搜索带有硒测试类的跨度

如何在自定义弹性搜索查询java中使用术语查询?

如何在Java中使用Lambda表达式搜索集合?

如何在NetBeans中使用Java在mongodb数据库中搜索条目?

如何在Java中使用rest API在弹性搜索中索引对象数组

如何在reactjs中使用框架显示搜索输入?

如何在ElasticSearch的嵌套查询中使用&堆叠搜索条件

如何在Rails 4中使用搜索表单

如何在Android中使用Spinner实施搜索?

如何在Kubernetes中使用VPA自动缩放弹性搜索?

如何在awk中使用substr搜索模式并提取