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

Content added Content deleted
(Created page with "<lang java> import java.util.ArrayList; import java.util.Collections; public class Utils<T extends Comparable<T>> { private void swap(ArrayList<T> data, int i, int j) { T t =...")
 
No edit summary
Line 3: Line 3:
import java.util.Collections;
import java.util.Collections;


public class Utils<T extends Comparable<T>> {
public class Utils {
private void swap(ArrayList<T> data, int i, int j) {
private static <T> void swap(ArrayList<T> data, int i, int j) {
T t = data.get(i);
T t = data.get(i);
data.set(i, data.get(j));
data.set(i, data.get(j));
Line 10: Line 10:
}
}


public ArrayList<Integer> mRange(int from, int to) {
public static ArrayList<Integer> mRange(int from, int to) {
ArrayList<Integer> result = new ArrayList<Integer>();
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i = from; i <= to; i++)
for (int i = from; i <= to; i++)
Line 17: Line 17:
}
}


public boolean nextPerm(ArrayList<T> data) {
public static <T extends Comparable<? super T>> boolean nextPerm(ArrayList<T> data) {
// find the swaps
// find the swaps
int c = -1, d = data.size();
int c = -1, d = data.size();
Line 43: Line 43:
}
}


public static <T extends Comparable<? super T>> ArrayList<ArrayList<T>> Permutations(ArrayList<T> d) {
@SuppressWarnings("unchecked")
public ArrayList<ArrayList<T>> Permutations(ArrayList<T> d) {
ArrayList<ArrayList<T>> result = new ArrayList<ArrayList<T>>();
ArrayList<ArrayList<T>> result = new ArrayList<ArrayList<T>>();
Collections.sort(d);
Collections.sort(d);
do {
do {
result.add((ArrayList<T>) d.clone());
result.add(new ArrayList<T>(d));
} while (this.nextPerm(d));
} while (nextPerm(d));
return result;
return result;
}
}