User:Margusmartsepp/Contributions/Java/Utils.java: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 8:
data.set(i, data.get(j));
data.set(j, t);
}
 
public static ArrayList<Integer> mRange(int from, int to) {
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i = from; i <= to; i++)
result.add(i);
return result;
}
 
Line 115 ⟶ 108:
return new ArrayList<T>(0);
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.
*/
public static ArrayList<Integer> mRange(int from, int to) {
if (from == to)
return new ArrayList<Integer>(0);
if (from < to) {
ArrayList<Integer> result = new ArrayList<Integer>(//
Math.abs(from - to) + 1);
for (int i = from; i <= to; i++)
result.add(i);
return result;
}
ArrayList<Integer> result = new ArrayList<Integer>();
Math.abs(from - to) + 1);
for (int i = from; i >= to; i--)
result.add(i);
return result;
}
}