Example usage for java.util Collections swap

List of usage examples for java.util Collections swap

Introduction

In this page you can find the example usage for java.util Collections swap.

Prototype

private static void swap(Object[] arr, int i, int j) 

Source Link

Document

Swaps the two specified elements in the specified array.

Usage

From source file:org.jiemamy.utils.collection.ListUtil.java

/**
 * ???index????????//from w  w  w . jav a  2  s. co m
 * 
 * @param list 
 * @param index 
 * @throws IllegalArgumentException ?{@code null}???
 * @throws IndexOutOfBoundsException if either index or index - 1 is out of range ((index - 1 < 0 || index >= list.size())).
 */
public static void moveUp(List<?> list, int index) {
    Validate.notNull(list);
    Collections.swap(list, index, index - 1);
}

From source file:org.jiemamy.utils.collection.ListUtil.java

/**
 * ??????/*  w  w w .  j  a  v a2s  .  co  m*/
 * 
 * @param list 
 * @throws IllegalArgumentException ?{@code null}???
 */
public static void reverse(List<?> list) {
    Validate.notNull(list);
    int center = list.size() / 2;
    int end = list.size() - 1;

    for (int i = 0; i < center; i++) {
        Collections.swap(list, i, end - i);
    }
}

From source file:edu.msu.cme.rdp.alignment.pairwise.PairwiseKNN.java

private static <T> void insert(T n, List<T> list, Comparator<T> comp, int k) {
    int i = list.size();
    list.add(n);// w  w  w .  jav  a 2s  . c o  m

    while (i > 0 && comp.compare(list.get(i), list.get(i - 1)) > 0) {
        Collections.swap(list, i, i - 1);
        i--;
    }

    if (list.size() > k) {
        list.remove(k);
    }
}

From source file:util.allocation.java

public static LinkedList<HOST> rankingHostlistFromCheapToHigh(LinkedList<String> HOSTIDList,
        LinkedList<HOST> HOSTList)

{
    LinkedList<HOST> rankedList = new LinkedList<HOST>();

    for (String currentHOSTID : HOSTIDList) {
        for (HOST currentHOST : HOSTList) {

            if (currentHOST.getID().equals(currentHOSTID))

            {//from   w  w  w.j a v a 2 s.  c  o  m
                rankedList.add(currentHOST);
            }

        }
    }

    int size = rankedList.size();
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {
            if (rankedList.get(i).getPrice() > rankedList.get(j).getPrice()) { // ??   
                Collections.swap(rankedList, i, j);
            }
        }
    }

    return rankedList;

}

From source file:bwem.util.Utils.java

public static <T> void fastErase(final List<T> list, final int index) {
    //        bwem_assert((0 <= i) && (i < (int)Vector.size()));
    if (!((0 <= index) && (index < list.size()))) {
        throw new IllegalArgumentException("index: " + index);
    }/*w  ww  .j a v a 2  s  .  co m*/

    Collections.swap(list, index, list.size() - 1);
    list.remove(list.size() - 1);
}

From source file:org.phoenicis.repository.types.MultipleRepository.java

public void moveRepository(Repository repository, int toIndex) {
    int oldIndex = this.repositories.indexOf(repository);

    Collections.swap(this.repositories, oldIndex, toIndex);
}

From source file:util.allocation.java

public static LinkedList<String> rankingHostlistFromCheapToHighAndReturnHOSTIDList(
        LinkedList<String> HOSTIDList, LinkedList<HOST> HOSTList)

{
    LinkedList<HOST> rankedList = new LinkedList<HOST>();

    for (String currentHOSTID : HOSTIDList) {
        for (HOST currentHOST : HOSTList) {

            if (currentHOST.getID().equals(currentHOSTID))

            {/*from www  .  j  a  v  a2  s. c o m*/
                rankedList.add(currentHOST);
            }

        }
    }

    int size = rankedList.size();
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {
            if (rankedList.get(i).getPrice() > rankedList.get(j).getPrice()) {
                Collections.swap(rankedList, i, j);
            }
        }
    }

    LinkedList<String> returnHOSTIDList = new LinkedList<String>();

    for (HOST currentHOST : rankedList) {
        returnHOSTIDList.add(currentHOST.getID());
    }

    return returnHOSTIDList;

}

From source file:com.xabber.android.ui.adapter.AccountListAdapter.java

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    if (fromPosition < toPosition) {
        for (int i = fromPosition; i < toPosition; i++) {
            Collections.swap(accountItems, i, i + 1);
        }/*from   w w w.j  a v  a2 s .  c o m*/
    } else {
        for (int i = fromPosition; i > toPosition; i--) {
            Collections.swap(accountItems, i, i - 1);
        }
    }
    notifyItemMoved(fromPosition, toPosition);
    return true;
}

From source file:de.kuschku.quasseldroid_ng.ui.coresettings.identity.nick.IdentityNickAdapter.java

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    Collections.swap(nicks, fromPosition, toPosition);
    notifyItemMoved(fromPosition, toPosition);
    return true;//from  ww  w  .j av a2 s.  co m
}

From source file:app.webelement.com.employeetracker.rd.RecyclerListAdapter.java

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    Collections.swap(mItems, fromPosition, toPosition);
    notifyItemMoved(fromPosition, toPosition);
    return true;/*from www . j  av  a 2 s.  c om*/
}