List of usage examples for java.util Collections swap
private static void swap(Object[] arr, int i, int j)
From source file:Main.java
public static void main(String[] args) { Vector<String> v = new Vector<String>(); v.add("1");/*from www. j ava 2 s .c o m*/ v.add("2"); v.add("3"); v.add("4"); v.add("java2s.com"); System.out.println(v); Collections.swap(v, 0, 4); System.out.println(v); }
From source file:Main.java
public static void main(String[] args) { Vector<String> v = new Vector<String>(); v.add("1");/*from w ww . j a v a 2s . co m*/ v.add("2"); v.add("3"); v.add("4"); v.add("5"); System.out.println(v); Collections.swap(v, 0, 4); System.out.println(v); }
From source file:Main.java
public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("A"); arrayList.add("B"); arrayList.add("C"); arrayList.add("D"); arrayList.add("E"); System.out.println(arrayList); Collections.swap(arrayList, 0, 4); System.out.println(arrayList); }
From source file:Main.java
public static void main(String[] args) { // create vector object List<String> vector = new ArrayList<String>(); // populate the vector vector.add("1"); vector.add("2"); vector.add("3"); vector.add("4"); vector.add("from java2s.com"); System.out.println("Before swap: " + vector); // swap the elements Collections.swap(vector, 0, 4); System.out.println("After swap: " + vector); }
From source file:Utilities.java
public static void main(String[] args) { List list = Arrays.asList("one Two three Four five six one".split(" ")); System.out.println(list);/*from w w w. ja v a2s. c om*/ System.out.println("max: " + Collections.max(list)); System.out.println("min: " + Collections.min(list)); AlphabeticComparator comp = new AlphabeticComparator(); System.out.println("max w/ comparator: " + Collections.max(list, comp)); System.out.println("min w/ comparator: " + Collections.min(list, comp)); List sublist = Arrays.asList("Four five six".split(" ")); System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist)); System.out.println("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist)); Collections.replaceAll(list, "one", "Yo"); System.out.println("replaceAll: " + list); Collections.reverse(list); System.out.println("reverse: " + list); Collections.rotate(list, 3); System.out.println("rotate: " + list); List source = Arrays.asList("in the matrix".split(" ")); Collections.copy(list, source); System.out.println("copy: " + list); Collections.swap(list, 0, list.size() - 1); System.out.println("swap: " + list); Collections.fill(list, "pop"); System.out.println("fill: " + list); List dups = Collections.nCopies(3, "snap"); System.out.println("dups: " + dups); // Getting an old-style Enumeration: Enumeration e = Collections.enumeration(dups); Vector v = new Vector(); while (e.hasMoreElements()) v.addElement(e.nextElement()); // Converting an old-style Vector // to a List via an Enumeration: ArrayList arrayList = Collections.list(v.elements()); System.out.println("arrayList: " + arrayList); }
From source file:Main.java
public static void swap(final List<?> list, final int index0, final int index1) { Collections.swap(list, index0, index1); }
From source file:Permutator.java
public static <T> Iterable<List<T>> permutations(final List<T> list) { return new Iterable<List<T>>() { public Iterator<List<T>> iterator() { return new Iterator<List<T>>() { private int current = 0; private final long length = factorial(list.size()); public List<T> next() { if (!hasNext()) { throw new NoSuchElementException(); }//from w w w .jav a2s . c o m List<T> permutation = new ArrayList<T>(list); int k = current; for (int j = 2; j <= list.size(); j++) { k /= j - 1; Collections.swap(permutation, (k % j), j - 1); } current++; return permutation; } public void remove() { throw new UnsupportedOperationException(); } public boolean hasNext() { return current < length; } }; } }; }
From source file:Main.java
/** Recursively creates permutations. */ private static <T> void permute(List<T> list, int index, List<List<T>> result) { for (int i = index; i < list.size(); i++) { Collections.swap(list, i, index); permute(list, index + 1, result); Collections.swap(list, index, i); }/* w w w. ja va 2s . c o m*/ if (index == list.size() - 1) { result.add(new ArrayList<T>(list)); } }
From source file:org.jiemamy.utils.collection.ListUtil.java
/** * ???index???????// w ww.j a v a2 s.co m * * @param list * @param index * @throws IllegalArgumentException ?{@code null}??? * @throws IllegalArgumentException index???? * @throws IndexOutOfBoundsException if either index or index + 1 is out of range ((index < 0 || index + 1 >= list.size())). */ public static void moveDown(List<?> list, int index) { Validate.notNull(list); Validate.isTrue(index >= 0); Collections.swap(list, index, index + 1); }
From source file:Main.java
/** * Auxilliary method required for merging two consecutive sorted lists in * place.//w ww. j av a2 s. co m * * <p>This implementation is based on:</p> * * <p>J. Chen, "<a href="http://dx.doi.org/10.1016/j.ipl.2005.11.018">A * simple algorithm for in-place merging</a>", Information Processing * Letters 98:34-40, 2006.</p>. * * This method is a direct transcription of Fig. 5. */ private static <T extends Comparable<? super T>> void mergeBandY(List<T> A, int z, int y, int yn) { while (z < y && y <= yn) { int j = z + indexOfMin(A.subList(z, y)); if (A.get(j).compareTo(A.get(y)) <= 0) { Collections.swap(A, z, j); } else { Collections.swap(A, z, y); y++; } z++; } if (z < y) { Collections.sort(A.subList(z, yn + 1)); } }