File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes problem about vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem about vector" Watch "problem about vector" New topic
Author

problem about vector

sheng zhong
Greenhorn

Joined: Nov 23, 2006
Posts: 20
import java.util.*;
public class test {

public static void main(String args[]){

Vector<Vector<Double>> Stree = new Vector<Vector<Double>>();
Vector<Double> Svec = new Vector<Double>();

Svec.add(100.0);
Stree.add(Svec);
Svec.add(200.0);
Stree.add(Svec);


System.out.println("Stree 0 is "+Stree.get(0));
System.out.println("Stree 1 is "+Stree.get(1));
}
}


The output are same, it's different from C++ result. I think it's the reason about java passing by reference, not a copy. So how can I do to make it output 100 and 200?
Nickolas Case
Ranch Hand

Joined: Apr 26, 2008
Posts: 98
I ran your code and got this as the output...

Stree 0 is [100.0, 200.0]
Stree 1 is [100.0, 200.0]

I am not sure what question you are asking? Are you only wanting just the 100.0 for Stree 0 and 200.0 for Stree 1? Or do you not want the decimals?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by sheng zhong:
... The output are same, it's different from C++ result. I think it's the reason about java passing by reference, not a copy. So how can I do to make it output 100 and 200?

Java does not pass by reference. It passes by value (reference copies).

I'm not sure what you mean by "make it output 100 and 200." Are you looking for something like this...?

Stree.get(0).get(0)
Stree.get(1).get(1)
[ April 26, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
sheng zhong
Greenhorn

Joined: Nov 23, 2006
Posts: 20
Hi, sorry for the misunderstanding. I am looking for the result like this:

Stree 0 is [100.0]
Stree 1 is [100.0] [200.0]

Since Svec changed before adding to Stree
[ April 26, 2008: Message edited by: sheng zhong ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

So instead of adding a Svec reference, you want to add a "snapshot" of what Svec looks like at the moment it's added.

I suppose you could clone Svec each time you add it.

[ April 26, 2008: Message edited by: marc weber ]
sheng zhong
Greenhorn

Joined: Nov 23, 2006
Posts: 20
Originally posted by marc weber:
So instead of adding a Svec reference, you want to add a "snapshot" of what Svec looks like at the moment it's added.

I suppose you could clone Svec each time you add it.


[ April 26, 2008: Message edited by: marc weber ]



Thanks, that's exactly the answer I am looking for.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem about vector
 
Similar Threads
Why is this an unchecked cast?
Vector is faster
whizlab question for SCJP1.5
problem different from c++
casting problem at runtime