ArrayList Retrieve object by Id

后端 未结 7 534
别跟我提以往
别跟我提以往 2020-12-16 12:49

Suppose I have an ArrayList of my custom Objects which is very simple. For example:

class Account
{
public String Name;
public In         


        
7条回答
  •  粉色の甜心
    2020-12-16 13:26

    Assuming that it is an unordered list, you will need to iterate over the list and check each object.

    for(int i = 0; i < sizeOfList; i++) {
        list.get(i).equals(/* What you compare against */)
    }
    

    There's also the other for syntax:

    for(Account a : accountList)
    

    You could put this loop into a helper method that takes an Account and compares it against each item.

    For ordered lists, you have more efficient search options, but you will need to implement a search no matter what.

提交回复
热议问题