ArrayList1 = {userid1, userid2, userid1, userid4, userid1, userid3, userid2, userid4, userid4, userid4, userid2};
ArrayList2 = {username1, username2, username3, username4};
映射這兩個數組所以,每當我打電話ArrayList1.get(0).getUserName()
,就應該爲我提供username1
。
ArrayList1 = {userid1, userid2, userid1, userid4, userid1, userid3, userid2, userid4, userid4, userid4, userid2};
ArrayList2 = {username1, username2, username3, username4};
映射這兩個數組所以,每當我打電話ArrayList1.get(0).getUserName()
,就應該爲我提供username1
。
有一種更好的方式來做到這一點,那就是通過使用HashMap
:
//create your custom object which will be mapped
public class User{
public String userId;
public String userName;
}
ArrayList<String> userKeys = new ArrayList<String>();
HashMap<String, User> users = new HashMap<String, User>();
現在使用USERKEY,您可以訪問其對應的用戶數據;
實施例:
User user = users.get("yourKey");
我認爲你應該使用:
List<List<T>> = ArrayList<ArrayList<T>>;
T爲類,你的用戶名。
public class User {
String username;
public User(String username)
{
this.username = username;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
}
userid1, userid2
...必須User
對象
User userid1 = new User("username1");
User userid2 = new User("username2");
初始化所有用戶對象
ArrayList1 = {userid1, userid2, userid1, userid4, userid1, userid3, userid2, userid4, userid4, userid4, userid2};
然後就可以調用
String username = ArrayList1.get(0).getUserName();
這將返回username1
請顯示您的嘗試。 – Maroun