ArrayList Limit to hold 10 Values

前端 未结 4 582
忘掉有多难
忘掉有多难 2021-01-04 17:51

I am using an ArrayList within my code which gets populated by a EditText field but I am wanting to limit the ArrayList so it can only

4条回答
  •  梦毁少年i
    2021-01-04 18:49

    You modify you setter method like this.

    private static ArrayList playerList = new ArrayList();
    
    public boolean addToPlayerList(String input) {
       if (playerList.size() < 10) {
          playerList.add(input);
          return true;   
       } else {
          return false;
       }
    }
    

    Or

    You can extend ArrayList and create your own customized class.

    public class MyArrayList extends ArrayList {
      @Override
      public boolean add(String input) {
          if (this.size() < 10) {
              return super.add(input);
          } else {
              return false;
          }
      }
    }
    

提交回复
热议问题