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 all = new ArrayList();
//这里构造一个值为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 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
#include
using namespace std;
class A{
public:
A(int aa) {
a = aa;
cout<
}
A(const A & another) {
cout<
a = another.a;
}
void out() {
cout<
}
int a;
};
void print(vector
for (int i = 0; i < vec.size(); i++) {
vec[i].out();
}
cout<
}
int main() {
vector
aVec.clear();
//弄1个值为1的a1,插入vector并打印
A a1(1);
aVec.push_back(a1);
print(aVec);
//改a1的值为2,再打印
a1.a = 2;
print(aVec);
//修改vector内部的元素的值,再打印
aVec[0].a = 3;
print(aVec);
return 0;
}
打印结果发现:
In Constructor(aa)
In Constructor(&A)
1
1
3
说明确实调用了拷贝构造函数,那么vector内部的对象aVec[0]和外部的对象a1自然是两个独立的对象了,自然对a1.a的任何修改对于aVec内的值没有影响,只有对vector里的东西的修改才有影响。
经过上述折腾,算是都搞清楚了。以后有不懂得问题,一定要照着全搞明白去搞,不要怕麻烦。
以上文字转自http://www.cnblogs.com/lihaozy/p/3182618.html
本文通过实例对比了Java的ArrayList与C++的vector在处理对象时的不同行为。Java的ArrayList通过引用传递对象,因此外部对对象的修改会影响ArrayList内的元素;而C++的vector通过拷贝构造函数创建新对象,因此外部对象的变化不会影响到vector中的元素。

802

被折叠的 条评论
为什么被折叠?



