本文整理汇总了Java中java.util.Collections.swap方法的典型用法代码示例。如果您正苦于以下问题:Java Collections.swap方法的具体用法?Java Collections.swap怎么用?Java Collections.swap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Collections
的用法示例。
在下文中一共展示了Collections.swap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onItemDragMoving
import java.util.Collections; //导入方法依赖的package包/类
public void onItemDragMoving(RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
int from = getViewHolderPosition(source);
int to = getViewHolderPosition(target);
if (from < to) {
for (int i = from; i < to; i++) {
Collections.swap(mData, i, i + 1);
}
} else {
for (int i = from; i > to; i--) {
Collections.swap(mData, i, i - 1);
}
}
notifyItemMoved(source.getAdapterPosition(), target.getAdapterPosition());
if (mOnItemDragListener != null && itemDragEnabled) {
mOnItemDragListener.onItemDragMoving(source, from, target, to);
}
}
示例2: setMainASMFile
import java.util.Collections; //导入方法依赖的package包/类
@Override
public void setMainASMFile()
{
ASMFile activeFile = getActiveFile();
if (activeFile == null)
{
Dialogues.showActionFailedDialogue("No file is selected!");
return;
}
Project activeProject = activeFile.getProject();
String message = "The file \"" + activeFile.getName()
+ "\" will be used as the main file for the project \""
+ activeProject.getName() + "\"";
Optional<ButtonType> result = Dialogues.showConfirmationDialogue(message);
if (result.get() == ButtonType.OK)
{
int index = activeProject.indexOf(activeFile);
Collections.swap(activeProject, 0, index);
}
}
示例3: onItemMove
import java.util.Collections; //导入方法依赖的package包/类
@Override
public boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
closeContextMenu(); // 关闭上下文菜单
// 不同的ViewType不能拖拽换位置。
if (srcHolder.getItemViewType() != targetHolder.getItemViewType()) return false;
int fromPosition = srcHolder.getAdapterPosition();
int toPosition = targetHolder.getAdapterPosition();
Collections.swap(eventList, fromPosition, toPosition); // 交换这两个对象
eventList.get(fromPosition).setPos(fromPosition); // 重新设置pos, Item根据pos排序
eventList.get(toPosition).setPos(toPosition); // 重新设置pos, Item根据pos排序
MyAdapter.notifyItemMoved(fromPosition, toPosition);
isSwap = true; // 标记发生了交换
return true; // 返回true表示处理了并可以换位置,返回false表示你没有处理并不能换位置。
}
示例4: selectSolution
import java.util.Collections; //导入方法依赖的package包/类
public ArrayList<Integer> selectSolution(){
iterationNumber++;
int actualCost= 0;
int bestCost = 10000000;
candidateSolution = clone(actualSolution);
for(int i = 0 ; i < size; i++){
for(int j = 0; j< i; j++){
Collections.swap(candidateSolution, i, j);
actualCost = getCost(candidateSolution);
Collections.swap(candidateSolution, j, i);
if(actualCost<bestCost){
bestCost = actualCost;
intercambioX = i;
intercambioY = j;
}
}
}
// System.out.println("El intercambio es " + intercambioX + " , " + intercambioY);
Collections.swap(candidateSolution, intercambioX, intercambioY);
return candidateSolution;
}
示例5: keySort
import java.util.Collections; //导入方法依赖的package包/类
public <T extends Comparable<T>> void keySort(
final List<Integer> indices, List<?> list) {
swapMap.clear();
for (int i = 0; i < indices.size(); i++) {
int k = indices.get(i);
while (swapMap.containsKey(k)) {
k = swapMap.get(k, 0);
}
swapMap.put(i, k);
}
for (IntIntMap.Entry e : swapMap) {
Collections.swap(list, e.key, e.value);
}
}
示例6: onMove
import java.util.Collections; //导入方法依赖的package包/类
@Override
public boolean onMove(int srcPosition, int targetPosition) {
if (shortCutList != null) {
// 更换数据源中的数据Item的位置
Collections.swap(shortCutList, srcPosition, targetPosition);
// 更新UI中的Item的位置,主要是给用户看到交互效果
rcvHomeAdapter.notifyItemMoved(srcPosition, targetPosition);
Log.i(TAG, "onMove: ---");
return true;
}
return false;
}
示例7: onItemMove
import java.util.Collections; //导入方法依赖的package包/类
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
// 当Item被拖拽的时候。
Collections.swap(searchEngines, fromPosition, toPosition);
mMenuAdapter.notifyItemMoved(fromPosition, toPosition);
SearchEngineUtil.getInstance().save(searchEngines);
return true;// 返回true表示处理了,返回false表示你没有处理。
}
示例8: permutate
import java.util.Collections; //导入方法依赖的package包/类
/**
* Permutate the list and store all possible permutations in the list of integer array.
*
*
* @param listofObjects the list of objects for which all possible permutations are required
* @param k the recursion parameter to keep track of all permutations
*/
public static void permutate(List<Integer> listofObjects, int k) {
for (int i = k; i < listofObjects.size(); i++) {
Collections.swap(listofObjects, i, k);
permutate(listofObjects, k + 1);
Collections.swap(listofObjects, k, i);
}
if (k == listofObjects.size() - 1) {
permutations.add(listofObjects.toArray(new Integer[listofObjects.size()]));
}
}
示例9: fireItemMoved
import java.util.Collections; //导入方法依赖的package包/类
public void fireItemMoved(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(data, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(data, i, i - 1);
}
}
}
示例10: calculateNextPermutation
import java.util.Collections; //导入方法依赖的package包/类
void calculateNextPermutation() {
j = list.size() - 1;
int s = 0;
// Handle the special case of an empty list. Skip the calculation of the
// next permutation.
if (j == -1) {
return;
}
while (true) {
int q = c[j] + o[j];
if (q < 0) {
switchDirection();
continue;
}
if (q == j + 1) {
if (j == 0) {
break;
}
s++;
switchDirection();
continue;
}
Collections.swap(list, j - c[j] + s, j - q + s);
c[j] = q;
break;
}
}
示例11: swapPositions
import java.util.Collections; //导入方法依赖的package包/类
/**
* Swap the item of list
*
* @param list data list
* @param from position from
* @param to position to
*/
public void swapPositions(List<?> list, int from, int to) {
if (customHeaderView != null) {
from--;
to--;
}
Collections.swap(list, from, to);
}
示例12: swapPositions
import java.util.Collections; //导入方法依赖的package包/类
/**
* Swap the item of list
*
* @param list data list
* @param from position from
* @param to position to
*/
public void swapPositions(List<?> list, int from, int to) {
if (hasHeaderView()) {
from--;
to--;
}
if (enableLoadMore() && to == getItemCount() - 1) return;
if (hasHeaderView() && to == 0) return;
if (hasHeaderView() && from == 0) return;
if (enableLoadMore() && from == getItemCount() - 1) return;
Collections.swap(list, from, to);
}
示例13: moveRowDown
import java.util.Collections; //导入方法依赖的package包/类
private void moveRowDown(TableViewer nameValueTableViewer)
{
Table table = nameValueTableViewer.getTable();
int[] indexes = table.getSelectionIndices();
for (int i = indexes.length - 1; i > -1; i--) {
if (indexes[i] < mappingSheetRow.getNameValueProperty().size() - 1) {
Collections.swap(mappingSheetRow.getNameValueProperty(), indexes[i], indexes[i] + 1);
nameValueTableViewer.refresh();
applyButton.setEnabled(true);
}
}
}
示例14: reorderItems
import java.util.Collections; //导入方法依赖的package包/类
@Override
public void reorderItems(int oldPosition, int newPosition) {
HashMap<String, Object> temp = list.get(oldPosition);
if (oldPosition < newPosition) {
for (int i = oldPosition; i < newPosition; i++) {
Collections.swap(list, i, i + 1);
}
} else if (oldPosition > newPosition) {
for (int i = oldPosition; i > newPosition; i--) {
Collections.swap(list, i, i - 1);
}
}
list.set(newPosition, temp);
}
示例15: onItemMove
import java.util.Collections; //导入方法依赖的package包/类
@Override
public void onItemMove(int fromPosition, int toPosition) {
boolean attemptingToSwapWithFooter = toPosition >= mBuildItems.size();
if (attemptingToSwapWithFooter) return;
boolean pointlessSwap = (fromPosition == toPosition);
if (pointlessSwap) return;
Collections.swap(mBuildItems, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
}