move Object Down in a List - Java Collection Framework

Java examples for Collection Framework:List

Description

move Object Down in a 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(moveDown(list, o));
    }//from ww w .j a  v a  2s .co  m

    @SuppressWarnings("unchecked")
    public static boolean moveDown(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