java Arrays.copyOf shallow copy

package com.robert.copy;


import java.util.Arrays;


public class TestBalloon {


public static void main(String[] args) {

int ballonNum = 7;

Balloon[] box1 = new Balloon[ballonNum];

for(int i = 0; i < ballonNum ; i ++) {

box1[i] = new Balloon(Color.values()[i], i);

}

// Balloon[] box2 = Arrays.copyOf(box1,  box1.length);    shallow copy 

Balloon[] box2 = new Balloon[box1.length];

for(int i = 0; i< box1.length; i++) {

box2[i] = new Balloon(box1[i].getColor(), box1[i].getId());

}

box2[6].setColor(Color.Blue);

for(Balloon b: box1) {

System.out.println(b);

}

}

}


enum Color {

Red,Orange,Yellow,Green,Indigo,Blue,Violet;

}


class Balloon {

private int id;

private Color color;

public int getId() {

return id;

}


public void setId(int id) {

this.id = id;

}


public Color getColor() {

return color;

}


public void setColor(Color color) {

this.color = color;

}


public Balloon(Color _color, int _id) {

color = _color;

id = _id;

}


@Override

public String toString() {

return "Balloon [id=" + id + ", color=" + color + "]";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值