Here you can find the source of moveItems(List list, int[] indices, int moveby, boolean up)
Parameter | Description |
---|---|
indices | the indices to move |
moveby | the number of items to move by |
up | if true then items are moved up, otherwise down |
protected static int[] moveItems(List list, int[] indices, int moveby, boolean up)
//package com.java2s; /*/*from w w w . ja v a 2 s .c om*/ * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.util.List; public class Main { /** * moves the selected items by a certain amount of items in a given direction. * * @param indices the indices to move * @param moveby the number of items to move by * @param up if true then items are moved up, otherwise down * @return the updated selected indices */ protected static int[] moveItems(List list, int[] indices, int moveby, boolean up) { int i; int newIndex; if (up) { for (i = 0; i < indices.length; i++) { if (indices[i] == 0) continue; newIndex = indices[i] - moveby; swap(list, indices[i], newIndex); indices[i] = newIndex; } } else { for (i = indices.length - 1; i >= 0; i--) { if (indices[i] == list.size() - 1) continue; newIndex = indices[i] + moveby; swap(list, indices[i], newIndex); indices[i] = newIndex; } } return indices; } /** * Swaps the two rows. * * @param firstIndex the index of the first row * @param secondIndex the index of the second row */ protected static void swap(List list, int firstIndex, int secondIndex) { Object backup; backup = list.get(firstIndex); list.set(firstIndex, list.get(secondIndex)); list.set(secondIndex, backup); } }