avatar
m*o
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));
}
}
avatar
h*o
2
vector in java is both sorted and ordered.
avatar
h*j
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);

avatar
h*j
4
害人啊

【在 h*********o 的大作中提到】
: vector in java is both sorted and ordered.
avatar
h*o
5
how?
avatar
h*o
6
my fault. it is only ordered.
avatar
m*o
7
怎么没一个人讲明白,VECTOR是动态同步的,所以都一样
可是为什么如果把VECTOR换成ARRAYLIST也是一样的结果?
avatar
g*g
8
我没细看你的程序,但是Vector跟ArrayList的区别就在于
多线程同步保护。你的程序只有一个线程,结果怎么都一样。

【在 m*******o 的大作中提到】
: 怎么没一个人讲明白,VECTOR是动态同步的,所以都一样
: 可是为什么如果把VECTOR换成ARRAYLIST也是一样的结果?

avatar
c*t
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);

avatar
c*t
10
Please do not give false / misleading information.

【在 h*********o 的大作中提到】
: my fault. it is only ordered.
avatar
m*o
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.

avatar
c*t
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?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。