Java List Move Item moveItems(List list, int[] indices, int moveby, boolean up)

Here you can find the source of moveItems(List list, int[] indices, int moveby, boolean up)

Description

moves the selected items by a certain amount of items in a given direction.

License

Open Source License

Parameter

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

Return

the updated selected indices

Declaration

protected static int[] moveItems(List list, int[] indices, int moveby, boolean up) 

Method Source Code


//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);
    }
}

Related

  1. moveElementInList(List list, int targetIndex, int sourceIndex)
  2. moveElementToIndex( List list, ELEMENT fromElement, ELEMENT toElement)
  3. moveInList(List list, int index, T listItem)
  4. moveItem(List list, Object oldItem, int newIndex)
  5. moveItemIdInSecond(final List list)
  6. moveLeft(int offset, T element, List list)
  7. moveOnePositionUp(final List list, final T object)
  8. moveRight(int offset, T element, List list)
  9. moveRows(final List allRows, final List rowsToMove, final int index)

  10. HOME | Copyright © www.java2s.com 2016