Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Collection;

import java.util.List;

public class Main {

    public static <T> Collection<T> move(List<T> collection, int fromPosition, int toPosition) {
        int maxPosition = collection.size() - 1;
        if (fromPosition == toPosition || fromPosition > maxPosition || toPosition > maxPosition)
            return collection;

        if (fromPosition < toPosition) {
            T fromModel = collection.get(fromPosition);
            T toModel = collection.get(toPosition);

            collection.remove(fromPosition);
            collection.add(collection.indexOf(toModel) + 1, fromModel);
        } else {
            T fromModel = collection.get(fromPosition);
            collection.remove(fromPosition);
            collection.add(toPosition, fromModel);
        }

        return collection;
    }
}