package com.sxt.arraytest2; /* * 形参列表的作用:1.接受方法调用处传来的实参 * 2.规定了实参传入数据的类型 */ import java.util.Arrays; public class TestArrrayDelete { public static void main(String[] args) { int[] arr = {32,45,76,23,7,13}; System.out.println("删除前:"+Arrays.toString(arr)); int index = 2;//删除下标为2的元素 Delete(arr, index);//[32, 45, 23, 7, 13, 0] int ele = 45; Search(arr,ele); } private static void Search(int[] arr, int ele) { int index = -1;//接收下标 for(int i=0; i<arr.length; i++){ if(arr[i] == ele){ index = i; break; } } if(index == -1){ System.out.println("不存在"); }else{ System.out.println(ele+"下标:"+index); } } private static void Delete(int[] arr, int index) { for(int i=index; i<arr.length-1; i++){//-1 arr[i+1]否则会越界 arr[i] = arr[i+1]; } arr[arr.length-1] = 0; System.out.println("删除后:"+Arrays.toString(arr)); } }
时间: 2024-10-14 19:05:05