Reputation: 1568
I'm not quite sure how to do this, but I find myself using a list of lists quite often in this particular project. I think I'd like to refactor this into it's own collection. Basically, it's a list of users and their attributes. The object I'm using is a List<List<String>>
. I think I'd like to make a drop-in replacement that maybe extends ArrayList
and call it UserList
or something similar.
Note: my only real goal is to keep from typing List<List<String>>
everywhere.
What would be the best approach to take with this?
EDIT: Thanks. I guess I knew something could be better. Just couldn't quite put my finger on it. I had initially avoided creating a User
class because I wasn't sure how I would easily be able to iterate over the attributes.
Upvotes: 0
Views: 3994
Reputation: 11132
You'll spend a lot more time writing this new class than just typing a few extra characters.
Why not like this?
public class User {
private String attribute1;
private String attribute2;
//etc.
public String getAttribute1() {
return attribute1;
}
public void setAttribute1(String value) {
attribute1 = value;
}
public String getAttribute2() {
return attribute2;
}
public void setAttribute2(String value) {
attribute2 = value;
}
//etc.
}
List
s.I suggest creating a wrapper class that would look something like this:
public class Users {
private List<List<String>> twoDList;
public String get(int x, int y) {
return twoDList.get(y).get(x);
}
public void set(int x, int y, String value) {
twoDList.set(y, twoDList.get(y).set(x, value));
}
//etc.
}
Upvotes: 3
Reputation: 178363
There is no need to create a custom List<List<String>>
for these requirements.
Create a User
class to store all the attributes, then create a List<User>
.
Upvotes: 5