move Object Up in the List - Java Collection Framework

Java examples for Collection Framework:List

Description

move Object Up in the List

Demo Code


//package com.java2s;
import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        List list = java.util.Arrays.asList("asdf", "java2s.com");
        Object o = "java2s.com";
        System.out.println(moveUp(list, o));
    }/*from   w  w  w. j  a v a2  s.c  o  m*/

    @SuppressWarnings("unchecked")
    public static boolean moveUp(List list, Object o) {
        int index = list.indexOf(o);
        if (index >= 0) {
            try {
                Object temp = list.set(index - 1, o);
                list.set(index, temp);
                return true;
            } catch (IndexOutOfBoundsException e) {
                return false;
            }
        }
        return false;
    }
}

Related Tutorials