User:Margusmartsepp/Contributions/Java/Utils.java: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
data.set(i, data.get(j)); |
data.set(i, data.get(j)); |
||
data.set(j, t); |
data.set(j, t); |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
Line 115: | Line 108: | ||
return new ArrayList<T>(0); |
return new ArrayList<T>(0); |
||
return L; |
return L; |
||
⚫ | |||
/** |
|||
* Creates a new {@code ArrayList} instance, containing input data. |
|||
* |
|||
* @param data |
|||
* List of mutable input elements. |
|||
* @return New {@link ArrayList} with input elements. |
|||
*/ |
|||
public static <T> ArrayList<T> aList(T... data) { |
|||
int capacity = 8 + data.length + (data.length >> 3); |
|||
ArrayList<T> list = new ArrayList<T>(capacity); |
|||
Collections.addAll(list, data); |
|||
return list; |
|||
} |
|||
/** |
|||
* |
|||
* Creates a new {@code ArrayList} instance, containing integer sequence |
|||
* between form and to. Sequence can be negative. |
|||
* |
|||
* @param from |
|||
* Integer with what sequence starts. |
|||
* @param to |
|||
* Integer with what sequence ends. |
|||
* @return List of mutable integer sequence. {@code if (from == to)}, then |
|||
* empty ArrayList is returned. |
|||
*/ |
|||
⚫ | |||
if (from == to) |
|||
return new ArrayList<Integer>(0); |
|||
if (from < to) { |
|||
ArrayList<Integer> result = new ArrayList<Integer>(// |
|||
Math.abs(from - to) + 1); |
|||
⚫ | |||
result.add(i); |
|||
return result; |
|||
} |
|||
⚫ | |||
Math.abs(from - to) + 1); |
|||
for (int i = from; i >= to; i--) |
|||
⚫ | |||
⚫ | |||
} |
} |
||
} |
} |