• 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
  • 相关阅读:
    mysql外键(FOREIGNKEY)使用介绍
    MYSQL数据库-约束
    mysql探究之null与not null
    爬虫
    http://blog.csdn.net/w_e_i_/article/details/70766035
    Python 3.5安装 pymysql 模块
    Python 3.5 连接Mysql数据库(pymysql 方式)
    hdu Bone Collector
    hdu City Game
    hdu Largest Rectangle in a Histogram
  • 原文地址:https://www.cnblogs.com/borter/p/9613374.html
Copyright © 2020-2023  润新知