java數組遍曆 刪除remove(範例程式碼)_java

來源:互聯網
上載者:User
廢話不多說,直接上代碼
複製代碼 代碼如下:

package com.b;

import java.util.ArrayList;

//數組遍曆刪除,添加
public class Core2 {
    private String name;
    private int num;
    private String color;

    public Core2() {
    }

    public Core2(String a, int b, String c) {
        name = a;
        num = b;
        color = c;
    }

    public String getName() {
        return name;
    }

    public String getColor() {
        return color;
    }

    public int getNum() {
        return num;
    }

    public static void main(String[] args) {
        Core2 c = new Core2("小紅", 13, "紅色");// 建立3個執行個體
        Core2 c2 = new Core2("小明", 12, "黃色");// 即對象
        Core2 c3 = new Core2("小強", 123, "藍色");
        Core2 c4 = new Core2("小li", 23, "白色");
        Core2 c5 = new Core2("小愛", 4, "blue");
        Core2 c6 = new Core2("小北", 2, "brave");
        ArrayList aa = new ArrayList();// 集合容器
        aa.add(c);
        aa.add(c2);
        aa.add(c3);
        aa.add(c4);
        // aa.remove(1);
        aa.add(2, c5);
        aa.add(1, c6);
        aa.remove(c4);
        System.out.println("大小為:" + aa.size());
        for (int i = 0; i < aa.size(); i++) {
            Core2 m = (Core2) aa.get(i);// arraylist的遍曆及增加
            System.out.println("第" + (i + 1) + "個學生姓名:" + m.getName() + ",學號是:"
                    + m.getNum() + ",顏色是:" + m.getColor());

        }

    }

}
大小為:5
第1個學生姓名:小紅,學號是:13,顏色是:紅色
第2個學生姓名:小北,學號是:2,顏色是:brave
第3個學生姓名:小明,學號是:12,顏色是:黃色
第4個學生姓名:小愛,學號是:4,顏色是:blue
第5個學生姓名:小強,學號是:123,顏色是:藍色

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.