论Java的ArrayList.add(e)和C++的vector.push_back(e)的区别
Java的ArrayList和C++的vector很类似,都是很基本的线性数据结构。但是他们的表现却不同。
在工作中碰到一个问题就是,搞不清楚到底传进去的是一个新对象,还是当前对象的引用!
经过实战分析:
在Java的ArrayList.add(e)中,传入的是引用,因此当你传入e以后,再改变e的成员,则ArrayList里的e也同样会改变,因为本身e和ArrayList中的e就是同一个东西。
而C++的vector.push_back(e)则会调用拷贝构造函数,因此当你传入e以后,再改变e的成员,则vector里的e不会变,因为已经是两个对象了。
Java代码:
import java.util.ArrayList;
public class TestJavaArrayListAdd {
public static void main(String[] args) {
ArrayList<A> all = new ArrayList<A>();
//这里构造一个值为1的a,插入all并打印
A a = new A(1);
all.add(a);
TestJavaArrayListAdd tester = new TestJavaArrayListAdd();
tester.print(all);
//改a的值为2,并再次打印all
a.memberA = 2;
tester.print(all);
}
private void print(ArrayList<A> all) {
for (int i = 0; i < all.size(); i++) {
System.out.print(all.get(i).memberA + " ");
}
System.out.println();
}
}
class A {
public int memberA;
A (int anotherMemberA) {
this.memberA = anotherMemberA;
}
}
运行如下:
1
2
可以看到,对于外面引用的改变对于ArrayList里面的元素也起作用了,下面来看看C++会怎么样。
C++代码:
#include <iostream>
#include <vector>
using namespace std; class A{
public: A(int aa) {
a = aa;
cout<<"In Constructor(aa)"<<endl;
} A(const A & another) {
cout<<"In Constructor(&A)"<<endl;
a = another.a;
} void out() {
cout<<a<<" ";
} int a;
}; void print(vector<A> &vec) {
for (int i = ; i < vec.size(); i++) {
vec[i].out();
}
cout<<endl;
} int main() {
vector<A> aVec;
aVec.clear(); //弄1个值为1的a1,插入vector并打印
A a1();
aVec.push_back(a1);
print(aVec); //改a1的值为2,再打印
a1.a = ;
print(aVec); //修改vector内部的元素的值,再打印
aVec[].a = ;
print(aVec);
return ;
}
打印结果发现:
In Constructor(aa)
In Constructor(&A)
1
1
3
说明确实调用了拷贝构造函数,那么vector内部的对象aVec[0]和外部的对象a1自然是两个独立的对象了,自然对a1.a的任何修改对于aVec内的值没有影响,只有对vector里的东西的修改才有影响。
经过上述折腾,算是都搞清楚了。以后有不懂得问题,一定要照着全搞明白去搞,不要怕麻烦。
论Java的ArrayList.add(e)和C++的vector.push_back(e)的区别的更多相关文章
- java之ArrayList.add
ArrayList添加 public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! ele ...
- 内功心法 -- java.util.ArrayList<E> (2)
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------下文主要对java.util ...
- Java ArrayList add(int index, E element) example
Simple add() method is used for adding an element at the end of the list however there is another va ...
- java 遍历arrayList的四种方法
package com.test; import java.util.ArrayList;import java.util.Iterator;import java.util.List; public ...
- 初涉java库--ArrayList
我的车就差一个轮子啦,造好轮子,我就飞上天与太阳肩并肩啦,想想都激动.什么你要自己造轮子,是不是傻,商店里不都是别人造好的吗,又好又方便,只需一点money,你没有money,那你只能做个安静的美男子 ...
- java集合-- arraylist小员工项目
import java.io.*; import java.util.ArrayList; public class Emexe { public static void main(String[] ...
- java 16-2 ArrayList的练习2
需求:去除集合中自定义对象的重复值(对象的成员变量值都相同 注意: 我们按照和字符串一样的操作,发现出问题了. 为什么呢? 我们必须思考哪里会出问题? 通过简单的分析,我们知道问题出现在了判断上. ...
- java 16-1 ArrayList的练习1
需求: ArrayList去除集合中字符串的重复值(去掉相同的字符串) 分析: 第一种做法:创建一个新的空集合: A:创建1个具有相同字符串的集合 B:创建1个空的集合 C:遍历第一个集合里面的元素 ...
- Java.util.ArrayList详解
java.util.ArrayList就是传说中的动态数组. 继承了关系,有此可看出ArrayList与list的collection的关系 public class ArrayList<E&g ...
随机推荐
- 八叉树(Octree)
八叉树(Octree)是一种用于描述三维空间的树状数据结构.想象一个立方体,我们最少可以切成多少个相同等分的小立方体?答案就是8个.再想象我们有一个房间,房间里某个角落藏着一枚金币,我们想很快的把金币 ...
- redis实现简单的分布式锁
在分布式系统中多个请求并发对少数资源进行争抢,例如10个人同时秒杀一件商品,如果不用分布式的锁进行处理(当然还有其它的处理方案),则很容易出现多个人抢到一个商品(超卖)的情况,用redis可以比较容易 ...
- python 之html的headers提取操作
# -*- coding: cp936 -*- #python 27 #xiaodeng #python 之html的headers提取操作 # import urllib,urllib2 html= ...
- P2093 零件分组【贪心算法练习题】
题目链接: http://codevs.cn/problem/4888/ https://www.luogu.org/problem/show?pid=2093 题目描述 某工厂生产一批棍状零件,每个 ...
- weblogic安装错误BEA-090870解决方案
00.问题描述 <Sep 3, 2017 3:29:09 PM CST> <Error> <Security> <BEA-090870> <The ...
- Mac 升级 PHP 7
http://www.phpyc.com/article/15 mac 自带 php, 这个地球人都知道 在新系统中,/usr/bin 成为了系统保护目录,所以我们以前使用的替换 系统 php 的方法 ...
- Android中如何让DialogFragment全屏(DialogFragment fullscreen)
这个问题很让人纠结,因为我要在popopwindows里面使用fragment,但是在popopwindows里面找不到它的父控件,于是转战使用DialogFragment,但是让它全屏是一件很头疼的 ...
- ios中两个view动画切换
@interface ViewController () @property(nonatomic,retain)UIView *redview; @property(nonatomic,retain) ...
- [转载]eclipse自动同步插件filesync的使用
原文地址:eclipse自动同步插件filesync的使用作者:老孙丢了金箍棒 这篇文章和之前我写的<eclipse下自动部署WEB项目>根本目的是一样的,只是达到目的的方式不同. ...
- C语言中的随意跳转
C语言中有一个很不常用的头文件:setjmp.h. 这个头文件是C语言底层实现的,不像math.h里面的函数都是纯C语言实现的. setjmp.h包含两个函数: longjmp 跳转到某个位置 set ...