posts - 45,comments - 0,views - 4781
复制代码
public static void main(String[] args) {
    //删除数组里面的所有a
    //数组元素都是连贯的
    //数组空间是不变的
    String[] strArray ={"a","b","a","a","d"};
    //null b null null d--->b d null null null
    System.out.println("删除之前:" + Arrays.toString(strArray));
    //判断 索引
 /*   int len = strArray.length;
    for(int index=0;index<len;index++){
        if("a".equals(strArray[index])){
            //后面元素循环遍历移位
            for(int newIndex =index; newIndex<len-1;newIndex++){
                strArray[newIndex] =strArray[newIndex+1]; 
           }
            strArray[len-1]=null;
            index--;
        }
    }*/
    int len =strArray.length;
    String[] newArray = new String[len];
    int i=0;
    for(int index=0;index<len;index++){
            String s = strArray[index];//把数据放到新数组中
             if(!"a".equals(s)){
                    //i新数组的索引
                    newArray[i++] =s;
    }
}
             strArray = newArray;//最好是一个数组,所以把新数组的数据赋给原数组
            System.out.println("删除之后:" + Arrays.toString(strArray));
}
复制代码

 

posted on   小贤看世界  阅读(142)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示