集册 Java实例教程 在列表中上移对象

在列表中上移对象

欢马劈雪     最近更新时间:2020-01-02 10:19:05

506
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在列表中上移对象


//package com.nowjava;
/*
N o w  J a v a  . c o m
*/

import java.util.List;


public class Main {

    public static void main(String[] argv) throws Exception {

        List list = java.util.Arrays.asList("asdf", "nowjava.com");

        Object o = "nowjava.com";

        System.out.println(moveUp(list, o));

    }


    @SuppressWarnings("unchecked")

    public static boolean moveUp(List list, Object o) {

        int index = list.indexOf(o);

        if (index >= 0) {/*来自 nowjava*/

            try {

                Object temp = list.set(index - 1, o);

                list.set(index, temp);

                return true;

      
展开阅读全文