ArrayList Limit to hold 10 Values

前端 未结 4 583
忘掉有多难
忘掉有多难 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条回答
  •  日久生厌
    2021-01-04 18:45

    If you don't have control over the function that adds elements to the list, you might want to override the ArrayList add.

    public class MySizeLimitedArrayList extends ArrayList {
      @Override
      public boolean add(Object e) {
          if (this.size() < 10) {
              return super.add(e);
          }
          return false;
      }
    }
    
        

    提交回复
    热议问题