集册 Java实例教程 从数组中移除项

从数组中移除项

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

523
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从数组中删除项目


//package com.nowjava;
/**
 * n o w j a v a . c o m - 时  代  Java 提 供 
**/


public class Main {

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

        String object = "nowjava.com";

        String[] array = new String[] { "1", "abc", "level", null,

                "nowjava.com", "asdf 123" };

        System.out.println(java.util.Arrays.toString(removeFromList(object,

                array)));

    }


    public static String[] removeFromList(String object, String[] array) {

        String[] ret = null;

        int remIndex = -1;

        try {

            for (int i = 0; i < array.length; i++) {

                if (object.equals(array[i])) {

                    remIndex = i;

                    break;

                }/** 时代Java公众号 提供 **/

            }

            if (remIndex != -1) {

                ret = new String[array.length - 1];

                int ri = 0;

                int wi = 0;

                while (wi < ret.length) {

                    if (ri == remIndex) {

                        ri++;

                    } else {

                        ret[wi] = array[ri];

                     
展开阅读全文