Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

22
LINES

< > BotCompany Repo | #1031197 // AutoGrowingList - this only grows on set

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2985L/18K).

persistable sclass AutoGrowingList<A> extends RandomAccessAbstractList<A> {
  new L<A> list;
  IF1<Int, A> createEmptyElement; // optional, takes index and produces element
  
  *(IF1<Int, A> *createEmptyElement) {}
  *(IF0<A> createEmptyElement) { this(if0ToIF1(createEmptyElement)); }
  
  public int size() { ret list.size(); }
  public A get(int i) { ret list.get(i); }
  
  public A set(int i, A a) {
    while (i > size())
      list.add(createEmptyElement?.get(i));
    if (i == size()) {
      add(a);
      null;
    } else
      ret list.set(i, a);
  };
  
  public bool add(A a) { ret list.add(a); }
}

Author comment

Began life as a copy of #1030633

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031197
Snippet name: AutoGrowingList - this only grows on set
Eternal ID of this version: #1031197/10
Text MD5: 5badc0313b4e16cd44fb40bfc2c8cf94
Transpilation MD5: a7cb4c58db8c31592117023727d531ce
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-05-20 02:42:48
Source code size: 633 bytes / 22 lines
Pitched / IR pitched: No / No
Views / Downloads: 228 / 431
Version history: 9 change(s)
Referenced in: #1031199 - AutoCreateList - create elements on get()
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)