使用 Java 将对象添加到 Vector 会丢失引用吗?

发布于 2024-08-23 23:18:34 字数 514 浏览 14 评论 0原文

我有一个包含许多对象的向量。我的代码使用循环根据特定条件将对象添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

岁吢 2024-08-30 23:18:34

每次都需要创建一个新的 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); with temp = new StringBuffer();

停顿的约定 2024-08-30 23:18:34

如果必须将一个独立对象添加到 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.

笑梦风尘 2024-08-30 23:18:34

它每次都使用相同的对象。您应该将 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.)

半山落雨半山空 2024-08-30 23:18:34

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 the Vector 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 with ArrayList (or one of the other classes implementing List, if you don't need an array backing it).

牛↙奶布丁 2024-08-30 23:18:34

将元素插入集合不会也不可能复制对象,因为 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.

时光是把杀猪刀 2024-08-30 23:18:34

正如这里大多数答案所说,Vector 存储对 Object 类型的对象的引用。如果每次更改底层Object,最终都会得到一个Vector,其中包含对一个对象的大量引用,该对象现在包含您为其提供的最后一个值。

根据变量的名称,我猜您实际上想在向量中存储数字。

在理想的情况下,您只需将 int 类型的对象添加到 Vector 中即可。不幸的是,在 Java 中,int 是一种“原始类型”,而 Vector 只能存储 Object 类型的对象。这意味着您只能将 Integer 对象放入 Vector,而不是 int 对象。

因此,您的代码将类似于:

// Put a number at index 0
Integer inputNumber = new Integer(7);
numbersToCalculate.add(0, inputNumber);

// Some time later read out the value at index 0
Object objFromVector = numbersToCalculate.elementAt(0);

// Cast the Object to an Integer
Integer numberToProcess = (Integer)objFromVector;

如果 Vector 包含的内容不是 Integer 对象,此代码将抛出 IllegalCastException。如果您担心,可以将其包含在 try catch 语句中。

在您的示例中,您可能只想循环遍历向量中的所有数字。您可能还希望更加规范 Vector 可以包含哪些对象(称为“泛型”,类似于 C 模板)。它可能如下所示:

Vector<Integer> myVector = new Vector<Integer>();

// Add stuff to the Vector

for (Integer number : myVector)
{
    // Do stuff with the number
}

foreach 和 Generics 构造仅在 Java SDK 1.5 中添加,因此如果您想在早期的 Java SDK 上运行,则无法使用它们。

As most of the answers here say, a Vector stores references to objects of type Object. If you change the underlying Object each time you will end up with a Vector 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 an int is a 'primitive type', and Vector can only store objects of type Object. This means you can only put Integer objects into the Vector instead of int objects.

So your code will look something like:

// Put a number at index 0
Integer inputNumber = new Integer(7);
numbersToCalculate.add(0, inputNumber);

// Some time later read out the value at index 0
Object objFromVector = numbersToCalculate.elementAt(0);

// Cast the Object to an Integer
Integer numberToProcess = (Integer)objFromVector;

This code will throw an IllegalCastException if the Vector contains something that isn't an Integer object. If you are worried about that you can encompass it in a try 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:

Vector<Integer> myVector = new Vector<Integer>();

// Add stuff to the Vector

for (Integer number : myVector)
{
    // Do stuff with the number
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文