简体   繁体   English

二维ArrayList在Java中的应用

[英]Application of 2D ArrayList in Java

I want to create a list of usernames, and then each username would correlate to a password. 我想创建一个用户名列表,然后每个用户名将与一个密码相关联。 The purpose is when a username is submitted, I would iterate through the username list to look for a match. 目的是在提交用户名时,我将遍历用户名列表以查找匹配项。 If matched, I would then ask for the password of that username. 如果匹配,那么我会要求输入该用户名的密码。 I thought of using a 2D ArrayList of String, but do not think it's possible to ask for a username and then check for the password that way. 我曾考虑过使用String的2D ArrayList,但不认为可以先问一个用户名,然后再检查密码。 Would the only possible (and not too advanced) way to do this be to create 2 separate ArrayList? 唯一可行的方法(不是太高级)是创建2个单独的ArrayList吗? If so, how do I make sure that for each name from username list, there is only one correct password in the password list? 如果是这样,我如何确保用户名列表中的每个名称在密码列表中只有一个正确的密码?

Note: I am just recently introduced to Java. 注意:我刚被介绍Java。

A map from java.util would do what you're looking for much better, you'd be able to do stuff like this: 来自java.util的地图可以更好地满足您的需求,您可以执行以下操作:

Map<String, String> namesToPasswords = new HashMap<>();
namesToPasswords.put("user1", "password");

Then namesToPasswords.get("user1") would return "password" which you can do what you please with. 然后, namesToPasswords.get("user1")将返回"password" ,您可以根据自己的namesToPasswords.get("user1")进行操作。

Not only is this a lot easier to think about than cross-referencing arrays, it's a lot faster too. 这不仅比交叉引用数组更容易思考,而且速度也更快。 Iterating over an array of length n to find a match is O(n), while looking up an entry in a HashMap is O(1). 遍历长度为n的数组以查找匹配项为O(n),而在HashMap查找条目的值为O(1)。

Documentation 文档

If by chance you do want to use a 2D ArrayList then this is one way you can do it: 如果您确实想使用2D ArrayList,则可以使用以下一种方法:

List<List<String>> users = new ArrayList<>(); 

// Fill the 2D List
users.add(new ArrayList<>()); users.get(0).add("Joe Blow"); users.get(0).add("password1");
users.add(new ArrayList<>()); users.get(1).add("John Doe"); users.get(1).add("password2");
users.add(new ArrayList<>()); users.get(2).add("Freddy Flint"); users.get(2).add("password3");
users.add(new ArrayList<>()); users.get(3).add("Tracey Johnson"); users.get(3).add("password4");
users.add(new ArrayList<>()); users.get(4).add("Mike Tyson"); users.get(4).add("I bit off his ear? :D");

// Provide a name that might be in the list
// User Name is case sensative. 
String suppliedUserName = "Mike Tyson";

// Iterate throught the list of User Names & Passwords...
String successMsg = "Can't Find User Named: " + suppliedUserName;
String password;
for (int i = 0; i < users.size(); i++) {
    if (users.get(i).get(0).equals(suppliedUserName)) {
        password = users.get(i).get(1);
        successMsg = "Hey..." + suppliedUserName + "'s Secret Password is: " + password;
        break; //found it so stop the iteration
    }
}

// Display a Success or Fail message.
System.out.println(successMsg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM