java数组--数组中删除指定某个值

1 Arrays.asList()

public static void main(String[] args) {
    String[] str={"11","22","33","44","55","66"};
    List<String> list=Arrays.asList(str);//将数组转换为list集合
    //*************************************
    //list.remove("11");
    //*************************************
    if(list.contains("22")){//加入集合中包含这个元素
        /*remove这些method时出现java.lang.UnsupportedOperationException异常。
         * 这是由于Arrays.asList() 返回java.util.Arrays$ArrayList,
         *  而不是ArrayList。Arrays$ArrayList和ArrayList都是继承AbstractList,
         *  remove,add等method在AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。
         *  ArrayList override这些method来对list进行操作,
         *  但是Arrays$ArrayList没有override remove(),add()等,
         *  所以throw UnsupportedOperationException。
         */
        //这个时候我们直接移除会报错,所以我们要转换为Arraylist
        //list.remove("张三");
        List<String> arrayList=new ArrayList<String>(list);//转换为ArrayLsit调用相关的remove方法
        arrayList.remove("33");
        for(String str1:arrayList ){
            System.out.print(str1+",");
        }
    }
}

2遍历数组

public static void main(String[] args)
{
    int[] arr = {0,0,12,1,0,4,6,0};
    arr = clearZero(arr);
    System.out.println("数组的元素:"+Arrays.toString(arr));
}


public static  int[] clearZero(int[] arr){
    //统计0的个数
    int count = 0; //定义一个变量记录0的个数
    for(int i = 0 ; i<arr.length ; i++){
        if(arr[i]==0){
            count++;
        }
    }

    //创建一个新的数组
    int[] newArr = new int[arr.length-count];

    int index  =0 ; //新数组使用的索引值
    //把非的数据存储到新数组中。
    for(int i = 0; i<arr.length ; i++){
        if(arr[i]!=0){
            newArr[index] = arr[i];
            index++;
        }
    }
    return newArr;
}

3字符串桥接

public static void main(String[] args) {
    int a2[] = { 1, 0, 5,0,6,0,4, 1, 0 };
    System.out.println("原数组:");
    for (int n : a2)
        System.out.print(n + ",");
    // 删除元素!
    a2 = value(a2, 0);
    System.out.println("\n现数组:");
    for (int n : a2)
        System.out.print(n + ",");

}
private static int[] value(int[] arr, int key) {
    StringBuilder str=new StringBuilder();
    for (int i = 0; i < arr.length; i++)
        if(arr[i]!=key)
            str.append(arr[i]);
    char[] chs=new String(str).toCharArray();
    int[] orr=new int[chs.length];
    for (int i = 0; i < orr.length; i++) {
        orr[i]=chs[i]-'0';
    }
    return arr=orr;
}

4 浅复制

public static void main(String[] args) {
    int a1[]={1,3,4,5,0,0,9,6,0,5,4,7,6,7,0,5};
    //测试第一种办法:
    System.out.println("原数组:");
    for (int n : a1)
        System.out.print(n + ",");
    // 删除元素!
    a1=volume(a1,0);
    System.out.println("\n现数组:");
    for (int n : a1)
        System.out.print(n + ",");

}
private static int[] volume(int[] arr, int key) {
    int count = 0;
    for (int i = 0, j = arr.length - 1; i <= j; i++, j--) {
        if (arr[i] == key && i != j)
            count++;
        if (arr[j] == key && i != j)
            count++;
        if (arr[i] == key && i == j)
            count++;
    }
    //上面就是计算0的个数
    int nrr[] = new int[arr.length - count];
    count = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == key)
            continue;
        else
            nrr[count++] = arr[i];
    }
    return arr = nrr;
}

公众号地址

全部评论

相关推荐

机械打工仔:不管啥专业,找工作改简历的第一课先把你那排版改了,简历上不要写个人简历四个字,找你要简历的谁不知道这个是简历?而且还占那么多空间,直接把自己名字和基础信息写上面,整体字体大一些。 还有这种经典两页简历一页大空白,导出PDF的时候多了一页几乎全是白的你自己看着不难受吗随手的事为啥不能改掉呢,这是态度问题,你试想一下你是HR你打开简历看到格式都没调整过会是什么感受?你自己都不重视你的简历,HR更不会在意。 然后内容你那个做两年咖啡就别往里写了,简历在精不在多,你在往你的简历里打字的时候就要想好这东西对你要找的工作有没有帮助。自我评价写一行就行了,不如给专业技能单开一栏。核心课程均分90这个真别写了,把你上过的有用的专业课列出来也行。有很多地方废话很多的精炼一下,比如你校内项目第一个写的那些,全然没有重点。 好好修改一下,我看你内容也挺优秀的,别被一个随便做的简历耽误了,我一个同专业的打工人看了都揪心更别说一天看几百份简历的HR
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务