m*******o 发帖数: 264 | 1 import java.util.*;
public class test {
public static void main(String args[]){
double S=100.0;
Vector> Stree = new Vector>();
Vector Svec = new Vector();
Svec.add(S);
Stree.add(Svec);
Svec.add(S+1);
Stree.add(Svec);
System.out.println("After loop Stree 0 is "+Stree.get(0));
System.out.println("After loop Stree 1 is "+Stree.get(1));
}
} |
h*********o 发帖数: 62 | 2 vector in java is both sorted and ordered. |
h**j 发帖数: 2033 | 3 SVec is a reference
【在 m*******o 的大作中提到】 : import java.util.*; : public class test { : public static void main(String args[]){ : double S=100.0; : : Vector> Stree = new Vector>(); : Vector Svec = new Vector(); : : Svec.add(S); : Stree.add(Svec);
|
h**j 发帖数: 2033 | 4 害人啊
【在 h*********o 的大作中提到】 : vector in java is both sorted and ordered.
|
h*********o 发帖数: 62 | |
h*********o 发帖数: 62 | 6 my fault. it is only ordered. |
m*******o 发帖数: 264 | 7 怎么没一个人讲明白,VECTOR是动态同步的,所以都一样
可是为什么如果把VECTOR换成ARRAYLIST也是一样的结果? |
g*****g 发帖数: 34805 | 8 我没细看你的程序,但是Vector跟ArrayList的区别就在于
多线程同步保护。你的程序只有一个线程,结果怎么都一样。
【在 m*******o 的大作中提到】 : 怎么没一个人讲明白,VECTOR是动态同步的,所以都一样 : 可是为什么如果把VECTOR换成ARRAYLIST也是一样的结果?
|
c*****t 发帖数: 1879 | 9 Well, in Java, all objects are pointers. So you are adding
the pointer to the same vector to Stree. Of course you will
get the same result because you get the same vector pointer.
In C++ vector, it always make a copy of what you pass, in
this case, the reference of the Svec. The result of course
would be different.
【在 m*******o 的大作中提到】 : import java.util.*; : public class test { : public static void main(String args[]){ : double S=100.0; : : Vector> Stree = new Vector>(); : Vector Svec = new Vector(); : : Svec.add(S); : Stree.add(Svec);
|
c*****t 发帖数: 1879 | 10 Please do not give false / misleading information.
【在 h*********o 的大作中提到】 : my fault. it is only ordered.
|
m*******o 发帖数: 264 | 11 Thanks a lot, but I remember all objects are reference in java, or pointers.
Correct me if I am wrong.
In this java case, is there any way to get the non-same result?
【在 c*****t 的大作中提到】 : Well, in Java, all objects are pointers. So you are adding : the pointer to the same vector to Stree. Of course you will : get the same result because you get the same vector pointer. : In C++ vector, it always make a copy of what you pass, in : this case, the reference of the Svec. The result of course : would be different.
|
c*****t 发帖数: 1879 | 12
pointers.
You can clone a copy then add.
I'd suggest you to study Java more systematically. It takes less
than a week to do so, but avoids confusions you might have for
a long run.
【在 m*******o 的大作中提到】 : Thanks a lot, but I remember all objects are reference in java, or pointers. : Correct me if I am wrong. : In this java case, is there any way to get the non-same result?
|