Java Code Examples for java.util.Collections.swap()

The following are Jave code examples for showing how to use swap() of the java.util.Collections class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: GitHub   File: BaseItemDraggableAdapter.java   View Source Code Vote up 7 votes
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);
    }
}
 
Example 2
Project: WebPLP   File: Main.java   View Source Code Vote up 6 votes
@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);
	}
}
 
Example 3
Project: EasyTodo   File: MainActivity.java   View Source Code Vote up 6 votes
@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表示你没有处理并不能换位置。
}
 
Example 4
Project: metaheuristics   File: Annealing.java   View Source Code Vote up 6 votes
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;

}
 
Example 5
Project: jbump   File: Collisions.java   View Source Code Vote up 6 votes
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);
  }
}
 
Example 6
Project: XposedNavigationBar   File: SetFunFragment.java   View Source Code Vote up 5 votes
@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;
}
 
Example 7
Project: Bigbang   File: SearchEngineActivity.java   View Source Code Vote up 5 votes
@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表示你没有处理。
}
 
Example 8
Project: jpl-framework   File: PermuatorCombinator.java   View Source Code Vote up 5 votes
/**
 * 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()]));
   }
}
 
Example 9
Project: Phoenix-for-VK   File: DrawerEditPresenter.java   View Source Code Vote up 5 votes
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);
        }
    }
}
 
Example 10
Project: sstore-soft   File: Collections2.java   View Source Code Vote up 5 votes
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;
  }
}
 
Example 11
Project: GitHub   File: NormalUltimateViewAdapter.java   View Source Code Vote up 5 votes
/**
 * 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);
}
 
Example 12
Project: GitHub   File: UltimateViewAdapter.java   View Source Code Vote up 5 votes
/**
 * 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);
}
 
Example 13
Project: Hydrograph   File: OperationClassDialog.java   View Source Code Vote up 5 votes
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);

		}
	}

}
 
Example 14
Project: PaoMovie   File: DragAdapter.java   View Source Code Vote up 5 votes
@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);
}
 
Example 15
Project: starcraft-2-build-player   File: EditBuildItemRecyclerAdapter.java   View Source Code Vote up 5 votes
@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);
}
 
Example 16
Project: RvAdapter   File: ItemTouchAdapter.java   View Source Code Vote up 5 votes
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    //交换数据源位置
    Collections.swap(getData(), fromPosition, toPosition);
    //交换列表中数据位置
    notifyItemMoved(fromPosition, toPosition);
    return true;
}
 
Example 17
Project: phonk   File: PCanvas.java   View Source Code Vote up 4 votes
public void move(int from, int to) {
    Collections.swap(layers, from, to);
}
 
Example 18
Project: Equella   File: GenericTable.java   View Source Code Vote up 4 votes
public void swapRows(int row1, int row2)
{
	Collections.swap(getEntries(), row1, row2);
	fireTableRowsUpdated(row1, row2);
}
 
Example 19
Project: WeatherPlus   File: LocationsListAdapter.java   View Source Code Vote up 4 votes
public void moveItem(int source, int target) {
    Collections.swap(mCities, source, target);
    notifyItemMoved(source, target);
}
 
Example 20
Project: ht-recyclerviewadapter   File: TBaseRecyclerViewAdapter.java   View Source Code Vote up 2 votes
/**
 * 交换两个数据条目的位置
 *
 * @param fromPosition
 * @param toPosition
 */
public void moveItem(int fromPosition, int toPosition) {
    Collections.swap(mItems, fromPosition, toPosition);
    notifyItemMoved(fromPosition, toPosition);
}