使用 Java 将对象添加到 Vector 会丢失引用吗?
我有一个包含许多对象的向量。我的代码使用循环根据特定条件将对象添加到 Vector。我的问题是,当我将对象添加到 Vector 时,原始对象引用是否添加到向量中,或者 Vector 是否创建该对象的新实例并添加它?
例如,在以下代码中:
private Vector numbersToCalculate;
StringBuffer temp = new StringBuffer();
while(currentBuffer.length() > i) {
//Some other code
numbersToCalculate.add(temp);
temp.setLength(0); //resets the temp StringBuffer
}
我正在做的是将“temp”StringBuffer 添加到numbersToCalculate Vector 中。我应该在循环中创建一个新的 StringBuffer 并添加它,还是这段代码可以工作?感谢您的帮助!
埃里克
I have a Vector that holds a number of objects. My code uses a loop to add objects to the Vector depending on certain conditions. My question is, when I add the object to the Vector, is the original object reference added to the vector or does the Vector make a new instance of the object and adds that?
For example, in the following code:
private Vector numbersToCalculate;
StringBuffer temp = new StringBuffer();
while(currentBuffer.length() > i) {
//Some other code
numbersToCalculate.add(temp);
temp.setLength(0); //resets the temp StringBuffer
}
What I'm doing is adding the "temp" StringBuffer to the numbersToCalculate Vector. Should I be creating a new StringBuffer within the loop and adding that or will this code work? Thanks for the help!
Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
每次都需要创建一个新的 StringBuffer。 Vector 中的每个项目项只是指向同一个 StringBuffer 对象的指针,因此每次循环时,您都会重置 stringbuffer 的单个实例并向 Vector 添加相同的引用。
只需将
temp.setLength(0);
替换为temp = new StringBuffer();
You need to create a new StringBuffer each time. Each item item in the Vector is just a pointer to the same StringBuffer object, so each time through the loop you are resetting the single instance of stringbuffer and adding the same reference to the Vector.
Just replace the
temp.setLength(0);
withtemp = new StringBuffer();
如果必须将一个独立对象添加到 Vector,请每次创建一个新对象。
您正在添加对向量的引用。如果对象的状态发生变化,则对该对象的所有引用都会看到该变化。
If you have to have an independent object added to the Vector, create a new one each time.
You're adding references to the vector. If the state of an object changes, then all references to it see the change.
它每次都使用相同的对象。您应该将
temp = new StringBuffer();
添加到循环末尾。 (循环的结果将是指向同一个空 StringBuffer 的指针向量。)It uses the same object each time. You should add a
temp = new StringBuffer();
to the end of your loop. (The result of your loop will be a Vector of pointers to the same single empty StringBuffer.)Vector
将存储您提供给它的引用,它不会创建自己的对象副本。因此,如果您希望 Vector 的缓冲区与您继续使用的缓冲区分开,正如您所说,您需要通过创建新缓冲区而不是设置旧缓冲区的长度来单独创建这些缓冲区为零。题外话:
Vector
已经相当过时了。你可能最好使用ArrayList< /code>
(或实现
List
,如果您不需要数组支持它)。The
Vector
will store the reference you give it, it won't create its own copy of the object. So if you want theVector
to have buffers separate from the one you're continuing to use, as you said you'll need to create those separately by creating a new buffer instead of setting the old one's length to zero.Off-topic side note:
Vector
is fairly out of date. you're probably better off withArrayList
(or one of the other classes implementingList
, if you don't need an array backing it).将元素插入集合不会也不可能复制对象,因为 Java 没有用户定义类型的复制构造函数或运算符重载的正式概念。也就是说,通用集合不知道如何复制所包含的对象。
Java 的赋值运算符总是复制用户定义类型的指针,而不是内容。
Inserting an element into a collection does not, and can not, make a copy of an object, because Java has no formalized notion of a copy-constructor or operator overloading for user-defined types. That is, a general purpose collection can not know how to copy the contained objects.
Java's assignment operator always copies the pointer, never the contents, of a user-defined type.
正如这里大多数答案所说,
Vector
存储对Object
类型的对象的引用。如果每次更改底层Object
,最终都会得到一个Vector
,其中包含对一个对象的大量引用,该对象现在包含您为其提供的最后一个值。根据变量的名称,我猜您实际上想在向量中存储数字。
在理想的情况下,您只需将
int
类型的对象添加到 Vector 中即可。不幸的是,在 Java 中,int
是一种“原始类型”,而Vector
只能存储Object
类型的对象。这意味着您只能将Integer
对象放入 Vector,而不是int
对象。因此,您的代码将类似于:
如果
Vector
包含的内容不是Integer
对象,此代码将抛出IllegalCastException
。如果您担心,可以将其包含在try
catch
语句中。在您的示例中,您可能只想循环遍历向量中的所有数字。您可能还希望更加规范 Vector 可以包含哪些对象(称为“泛型”,类似于 C 模板)。它可能如下所示:
foreach
和 Generics 构造仅在 Java SDK 1.5 中添加,因此如果您想在早期的 Java SDK 上运行,则无法使用它们。As most of the answers here say, a
Vector
stores references to objects of typeObject
. If you change the underlyingObject
each time you will end up with aVector
containing lots of references to one object, which now contains the last value you gave it.Based on the name of your variables, I'm guessing you actually want to be storing numbers in your Vector.
In an ideal world you would just add object of type
int
into the Vector. Unfortunately, in Java anint
is a 'primitive type', andVector
can only store objects of typeObject
. This means you can only putInteger
objects into the Vector instead ofint
objects.So your code will look something like:
This code will throw an
IllegalCastException
if theVector
contains something that isn't anInteger
object. If you are worried about that you can encompass it in atry
catch
statement.In your example you will presumably want to just loop through all the numbers in the Vector. You also might want to be more prescriptive about what objects your Vector can contain (called 'Generics', which is similar to C templating). Here's what it might look like:
The
foreach
and Generics constructs were only added in Java SDK 1.5, so you can't use them if you want to run on an earlier Java SDK.