class="java"> package com.zas.util; import java.util.Arrays; public class ArrayUtil { /** * 删除数组的第 i 个元素(从0开始) * @param arr * @param pos * @return 成功 true 失败 false */ public static Boolean arrayDeleteByPos(Object[] arr, int pos) { if(arr == null || pos < 0 || (pos + 1) > arr.length){ return false; } for (int j = pos; j < arr.length - 1; j++) { arr[j]= arr[j + 1]; } arr[arr.length - 1] = null; return true; } /** * 删除数组元素 obj (相同元素只删除一个) * @param arr * @param i * @return 成功 true 失败 false */ public static Boolean arrayDeleteByObj(Object[] arr, Object obj) { if(arr == null){ return false; } int pos = -1; for (int j = 0; j < arr.length; j++) { if(obj == null){ if(arr[j] == null){ pos = j; } }else{ if(obj.equals(arr[j])){ pos = j; } } } if(pos == -1){ return false; } return arrayDeleteByPos(arr, pos); } public static void main(String[] args) { Object[] mks = {1, "2", 3, "4", 5, 6}; System.out.println(Arrays.toString(mks)); arrayDeleteByObj(mks, 4); System.out.println(Arrays.toString(mks)); arrayDeleteByObj(mks, "4"); System.out.println(Arrays.toString(mks)); arrayDeleteByPos(mks, 2); System.out.println(Arrays.toString(mks)); } }