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

51
LINES

< > BotCompany Repo | #1000999 - asList - always returns a new ArrayList (no wrapping)

JavaX fragment (include)

static <A> ArrayList<A> asList(A[] a) {
  return a == null ? new ArrayList<A>() : new ArrayList<A>(Arrays.asList(a));
}

static ArrayList<Integer> asList(int[] a) {
  if (a == null) null;
  ArrayList<Integer> l = emptyList(a.length);
  for (int i : a) l.add(i);
  ret l;
}

static ArrayList<Float> asList(float[] a) {
  if (a == null) null;
  ArrayList<Float> l = emptyList(a.length);
  for (float i : a) l.add(i);
  ret l;
}

static ArrayList<Double> asList(double[] a) {
  if (a == null) null;
  ArrayList<Double> l = emptyList(a.length);
  for (double i : a) l.add(i);
  ret l;
}

static <A> ArrayList<A> asList(Iterable<A> s) {
  if (s instanceof ArrayList) return (ArrayList) s;
  ArrayList l = new ArrayList();
  if (s != null)
    for (A a : s)
      l.add(a);
  return l;
}

ifclass Producer
static <A> ArrayList<A> asList(Producer<A> p) {
  new ArrayList l;
  A a;
  if (p != null) while ((a = p.next()) != null)
    l.add(a);
  ret l;
}
endif

static <A> ArrayList<A> asList(Enumeration<A> e) {
  new ArrayList l;
  if (e != null)
    while (e.hasMoreElements())
      l.add(e.nextElement());
  return l;
}

download  show line numbers  debug dex   

Travelled to 13 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, tslmcundralx, tvejysmllsmz, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000999
Snippet name: asList - always returns a new ArrayList (no wrapping)
Eternal ID of this version: #1000999/10
Text MD5: e134c3e3a3cc0adba58ca7b74d9ca644
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-02 16:50:46
Source code size: 1166 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 589 / 4401
Version history: 9 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1000999 & http://1000999.tinybrain.de