2012-08-27 65 views
0

ArrayList group = new ArrayList();如何將多維數據存儲在ArrayList中

group.add(「John Smith」,「5126123」);

我想存儲2個數據。所以我可以通過名字來獲得名字的數字和數字。這是可能的ArrayList和如何?

*和不同的人可以有相同的groupnumber

+1

你的問題有點過時了。你的名字和號碼總是獨一無二的嗎?或者只有數字OR名稱是唯一的?如果您的數字OR名稱是唯一的,您應該使用Map而不是ArrayList。否則,你必須開始在ArrayList中使用一個ArrayList,所以它是二維的。 – 2012-08-27 07:54:00

+0

讓我來寫一個例子: group.add(「John Smith」,「1」); group.add(「Elis Smith」,「1」); group.add(「Michael Smith」,「1」); 組。添加(「Luuk Daves」,「2」); – user1621988

+0

您可以創建一個HashMap,其中的數字是鍵,並將屬於該鍵的名稱存儲到ArrayList中,因此您將擁有一個HashMap >。但是,您將不得不迭代您的哈希映射來搜索ArrayList中的名稱。 – 2012-08-27 08:18:27

回答

0

你可以一個數組列表內存儲陣列,像這樣 group.add(new String[][] {"John Smith", "5125123"});

但是,我不知道是多麼有用的會是在幫助你完成你想要完成的任何事情。

另一種方法是使用哈希表或某些其他實現的地圖/字典數據類型,其中包含<key, value>對。但是,搜索僅限於使用密鑰。

另一種方法是創建一個對象來容納John Smith及其相關信息,然後將該對象放入arraylist中。你必須編寫一些額外的代碼來幫助你搜索和檢索你的數據。

+0

我該如何實現這一目標? \t \t ArrayList group = new ArrayList (); \t \t group.add(new String [] [] {「John Smith」,「5125123」}); and should group.get(「John Smith」) - > 5125123 group.get(「5125123」) - > John Smith(和該組的其他成員) – user1621988

1

您可以用這種方式使用ArrayList: -

List<Map<String, String>> list = new ArrayList<Map<String, String>>(); 
       Map<String, String> map1 = new HashMap<String, String>(); 
       map1.put("John Smith","5126123"); 
         list.add(map1); 

也可以直接使用地圖: -

Map<String,Integer> map = new HashMap<String,Integer>(); 
map.put("Some String", 42); 
// or, more correctly: 
map.put("Some String", Integer.valueOf(42)); 

可以使用

Integer result = map.get("Some String"); 
1

簡單的解決辦法尋找它(但可能不是最有效的): 這不是在IDE中輸入的,所以在這裏可能有拼寫錯誤。

公共類用戶{

private int id; 
private String name; 

public User() {}; 

public User(int id, String name) { 
    this.id = id; 
    this.name = name; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public int getId() { 
    return id; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getName() { 
    return name; 
} 

}

公共類MyUserArray(){

private ArrayList<User> users; 

public MyUserArray() { 
    users = new ArrayList<User>(); 
} 

public add() { 
    users.add(user); 
} 

// Since you said the user name is unique, only 
// one user will be returned 
public User getUserByName(String name) { 
    for(User user : users) { 
     if(user.getName().equalsIgoreCase(name)) 
      return user; 
    } 

    // return null if no user is found 
    return null; 
} 

// Since more users can have the same id, we will 
// return an array list with results 
public ArrayList<User> getUsersById(int id) { 

    ArrayList<User> result = new ArrayList<User>(); 

    for(User user : users) { 
     if(user.getId() == id) 
      result.add(user); 
    } 

    return result; 
} 

}

公共MyClass類{

public static void main(String[] args) { 
    // Create users 
    User user1 = new User(1, "John Doe"); 
    User user2 = new User(1, "Jack Sparrow"); 
    User user3 = new User(2, "Mickey Mouse"); 

    // Create the personal user array 
    MyUserArray users = new MyUserArray(); 

    // Add the users 
    users.add(user1); 
    users.add(user2); 
    users.add(user3); 

    // Now you can search on id: 
    ArrayList<User> userArrayList = users.getUsersById(1); 
    for (User user : userArrayList) { 
     System.out.println(user.getId() + " - " + user.getName()); 
     // This will print: 
     // 1 - John Doe 
     // 1 - Jack Sparrow 
    } 

    // Or search on name 
    User user = users.getUserByName("Jack Sparrow"); 
    System.out.println(user.getId() + " - " + user.getName()); 
    // This will print: 1 - Jack Sparrow 
} 

}

相關問題