zoukankan      html  css  js  c++  java
  • Java如何删除数组中的元素?

    Java中,如何删除数组元素?

    示例

    以下示例显示如何从数组中删除元素。

    package com.yiibai;
    
    import java.util.ArrayList;
    
    public class RemoveArrayElement {
        public static void main(String[] args) {
            ArrayList objArray = new ArrayList();
            objArray.clear();
            objArray.add(0, "0th element");
            objArray.add(1, "1st element");
            objArray.add(2, "2nd element");
            System.out.println("Array before removing an element" + objArray);
            objArray.remove(1);
            objArray.remove("0th element");
            System.out.println("Array after removing an element" + objArray);
        }
    }
    
    Java

    执行上面示例代码,得到以下结果 -

    Array before removing an element[0th element, 1st element, 2nd element]
    Array after removing an element[2nd element]
    
    Shell

    示例2

    数组删除的另一个示例示例

    package com.yiibai;
    
    import java.util.*;
    
    public class RemoveArrayElement2 {
        public static void main(String[] args) {
            ArrayList<Integer> arr = new ArrayList<Integer>(5);
            arr.add(220);
            arr.add(115);
            arr.add(320);
            arr.add(145);
    
            System.out.println("Size of list: " + arr.size());
            for (Integer number : arr) {
                System.out.println("Number = " + number);
            }
            arr.remove(2);
    
            System.out.println("After remove, Size of list: " + arr.size());
    
            for (Integer number : arr) {
                System.out.println("Number = " + number);
            }
        }
    }
    
    Java

    执行上面示例代码,得到以下结果 -

    Size of list: 4
    Number = 220
    Number = 115
    Number = 320
    Number = 145
    After remove, Size of list: 3
    Number = 220
    Number = 115
    Number = 145
  • 相关阅读:
    词频统计
    第二周每周例行报告
    事务管理(ACID)
    Redis
    jar包和war包的区别
    CSS实现Loading加载动画
    如何实现“返回顶部”的页面效果
    PHP页面跳转-常见方法
    局域网络调试方式
    Thinkphp 统计数据库字段总值
  • 原文地址:https://www.cnblogs.com/borter/p/9613374.html
Copyright © 2011-2022 走看看