java.util.Vector的奇怪行为 [英] Strange behavior of a java.util.Vector

查看:59
本文介绍了java.util.Vector的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否会生气或愚蠢但是我可以依赖我的眼睛上的
,当我将一个元素添加到Vector时,它会被放置

所有位置,而不是最后一个位置。这就好比如果它沿着列表的方向滚动所有

一直留下它的足迹。

我写的
,如:

import java.util。*;


public class Temp {

public static void main(String [] arg){

Vector vec = new Vector();

String [] outPutLine = new String [3];

String line;

for(int k = 0; k <4; k ++){

for(int i = 0; i< outPutLine.length; i ++)

outPutLine [i ] =" S(" + i +" ;;" + k +")" ;;

vec.add(outPutLine);


System.out.println(" outPutLine" + k +" is:\t\t" + outPutLine [0] +

" \t" + outPutLine [ 1] +" \t" + outPutLine [2]);

System.out.println("结果是:");


String test ="" ;;

for(int i = 0; i< vec.size(); i ++){

for(int j = 0; j <3; j ++)

test + =((String [])vec.get(i))[j];

test + =" \ n" ;;

}

System.out.println(test +" \\\
------------------------------ --- \ n");

}

}

}


所以,它只是我还是有输出的时髦?例如,

元素S(0; 0)去了哪里?!


-


请支付

Konrad

-------------------------------- -------------------

愿所有垃圾邮件发送者死于痛苦的死亡;没有埋葬地点;

他们的灵魂会被Gehenna的恶魔从一个房间追逐到

另一个永恒和更多。


睡眠 - 无效人士使用的东西

作为咖啡的替代品


野心 - 一个糟糕的借口没有

足够的感觉是懒惰的

------------------------------------- --------------

I''m not sure if i''m going mad or stupid but as far as i can rely
on my eyes, when i ADD an element to a Vector, it gets put on
ALL the position, instead of the last one. It''s like if it rolls all
the way down the list leaving its footprints along the way.

I wrote, like:
import java.util.*;

public class Temp {
public static void main (String[] arg) {
Vector vec = new Vector ();
String[] outPutLine = new String[3];
String line;
for (int k = 0; k < 4; k++) {
for (int i = 0; i < outPutLine.length; i++)
outPutLine[i] = "S(" + i + ";" + k + ")";
vec.add (outPutLine);

System.out.println ("outPutLine " + k + " is: \t\t" + outPutLine[0] +
"\t" + outPutLine[1] + "\t" + outPutLine[2]);
System.out.println ("The result is:");

String test = "";
for (int i = 0; i < vec.size (); i++) {
for (int j = 0; j < 3; j++)
test += ((String[])vec.get (i))[j];
test += "\n";
}
System.out.println (test + "\n---------------------------------\n");
}
}
}

So, is it just me or is there funky with the output? Where did the
element S(0;0) go, for instance?!

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------


推荐答案

请记住在Java中所有对象和数组实际上是引用

(像指针一样)。因此,您将三次添加相同的对数组的引用

(即数组outPutLine)。

Vector的所有三个元素都指向同一个数组。


HTH,问候,

Jan van Mansum。


Konrad Den Ende写道:
Remember that in Java all objects and arrays are actually references
(like pointers). So you are adding the same "reference to an array"
(namely the array "outPutLine") three times. All three elements of the
Vector will point to the same array.

HTH, regards,
Jan van Mansum.

Konrad Den Ende wrote:
我不确定我是疯了还是傻,但据我可以依赖
在我的眼睛上,当我将一个元素添加到Vector时,它会被置于所有位置,而不是最后一个位置。这就好比如果它沿着列表向下滚动所有的路径留下它的足迹。

我写道,如:
import java.util。*; <公共类Temp {
public static void main(String [] arg){
Vector vec = new Vector();
String [] outPutLine = new String [ 3];
字符串行;
for(int k = 0; k <4; k ++){
for(int i = 0; i< outPutLine.length; i ++)
outPutLine [i] =" S(" + i +" ;;" + k +")" ;;
vec.add(outPutLine);

System.out.println(" outPutLine" + k +"是:\t \ t" + outPutLine [0] +
" \t" + outPutLine [1] +" \\ \\ t" + outPutLine [2]);
System.out.println("结果是:");

字符串测试="" ;;
for(int i = 0; i< vec.size(); i ++){
for(int j = 0; j< 3; j ++)
test + =((String []) vec.get(i))[j];
测试+ =" \\\
" ;;
}
System.out.println(test +" \\\
------------------- -------------- \\\
");
}
}
}

所以,这只是我还是输出有时髦吗?例如,
元素S(0; 0)在哪里?!
I''m not sure if i''m going mad or stupid but as far as i can rely
on my eyes, when i ADD an element to a Vector, it gets put on
ALL the position, instead of the last one. It''s like if it rolls all
the way down the list leaving its footprints along the way.

I wrote, like:
import java.util.*;

public class Temp {
public static void main (String[] arg) {
Vector vec = new Vector ();
String[] outPutLine = new String[3];
String line;
for (int k = 0; k < 4; k++) {
for (int i = 0; i < outPutLine.length; i++)
outPutLine[i] = "S(" + i + ";" + k + ")";
vec.add (outPutLine);

System.out.println ("outPutLine " + k + " is: \t\t" + outPutLine[0] +
"\t" + outPutLine[1] + "\t" + outPutLine[2]);
System.out.println ("The result is:");

String test = "";
for (int i = 0; i < vec.size (); i++) {
for (int j = 0; j < 3; j++)
test += ((String[])vec.get (i))[j];
test += "\n";
}
System.out.println (test + "\n---------------------------------\n");
}
}
}

So, is it just me or is there funky with the output? Where did the
element S(0;0) go, for instance?!



请记住,在Java中,所有对象和数组实际上都是引用

(像指针一样)。因此,您将三次添加相同的对数组的引用

(即数组outPutLine)。

Vector的所有三个元素都指向同一个数组。


HTH,问候,

Jan van Mansum。


Konrad Den Ende写道:
Remember that in Java all objects and arrays are actually references
(like pointers). So you are adding the same "reference to an array"
(namely the array "outPutLine") three times. All three elements of the
Vector will point to the same array.

HTH, regards,
Jan van Mansum.

Konrad Den Ende wrote:
我不确定我是疯了还是傻,但据我可以依赖
在我的眼睛上,当我将一个元素添加到Vector时,它会被置于所有位置,而不是最后一个位置。这就好比如果它沿着列表向下滚动所有的路径留下它的足迹。

我写道,如:
import java.util。*; <公共类Temp {
public static void main(String [] arg){
Vector vec = new Vector();
String [] outPutLine = new String [ 3];
字符串行;
for(int k = 0; k <4; k ++){
for(int i = 0; i< outPutLine.length; i ++)
outPutLine [i] =" S(" + i +" ;;" + k +")" ;;
vec.add(outPutLine);

System.out.println(" outPutLine" + k +"是:\t \ t" + outPutLine [0] +
" \t" + outPutLine [1] +" \\ \\ t" + outPutLine [2]);
System.out.println("结果是:");

字符串测试="" ;;
for(int i = 0; i< vec.size(); i ++){
for(int j = 0; j< 3; j ++)
test + =((String []) vec.get(i))[j];
测试+ =" \\\
" ;;
}
System.out.println(test +" \\\
------------------- -------------- \\\
");
}
}
}

所以,这只是我还是输出有时髦吗?例如,
元素S(0; 0)在哪里?!
I''m not sure if i''m going mad or stupid but as far as i can rely
on my eyes, when i ADD an element to a Vector, it gets put on
ALL the position, instead of the last one. It''s like if it rolls all
the way down the list leaving its footprints along the way.

I wrote, like:
import java.util.*;

public class Temp {
public static void main (String[] arg) {
Vector vec = new Vector ();
String[] outPutLine = new String[3];
String line;
for (int k = 0; k < 4; k++) {
for (int i = 0; i < outPutLine.length; i++)
outPutLine[i] = "S(" + i + ";" + k + ")";
vec.add (outPutLine);

System.out.println ("outPutLine " + k + " is: \t\t" + outPutLine[0] +
"\t" + outPutLine[1] + "\t" + outPutLine[2]);
System.out.println ("The result is:");

String test = "";
for (int i = 0; i < vec.size (); i++) {
for (int j = 0; j < 3; j++)
test += ((String[])vec.get (i))[j];
test += "\n";
}
System.out.println (test + "\n---------------------------------\n");
}
}
}

So, is it just me or is there funky with the output? Where did the
element S(0;0) go, for instance?!



Konrad Den Ende写道:
Konrad Den Ende wrote:
我不确定我是否会生气或愚蠢但是我可以依赖我的眼睛,当我将一个元素添加到Vector时,它会被放到
所有位置,而不是最后一个位置。这就好比如果它沿着列表向下滚动所有的路径留下它的足迹。

我写道,如:
import java.util。*; <公共类Temp {
public static void main(String [] arg){
Vector vec = new Vector();
String [] outPutLine = new String [ 3];


请注意,只有一个数组outPutLine,它的值永远不会改变:

它总是引用3个字符串的相同数组。 br />
字符串行;
for(int k = 0; k <4; k ++){
for(int i = 0; i< outPutLine.length; i ++)
outPutLine [i] =" S(" + i +" ;;" + k +")" ;;
vec.add(outPutLine);

System.out.println(" outPutLine" + k +"是:\t \ t" + outPutLine [0] +
" \t" + outPutLine [1] +" \\ \\ t" + outPutLine [2]);
System.out.println("结果是:");


让我们打开外循环。

int k = 0;

for(int i = 0; i < outPutLine.length; i ++)

outPutLine [i] =" S(" + i +";" + k +")" ;;

vec.add(outPutLine);

// [...]

}

k = 1;

for(int i = 0; i< outPutLine.length; i ++)

outPutLine [i] =" S(" + i +" ;;" + k +" )" ;;

//请注意,我们正在从之前的

迭代中覆盖outPutLine的元素

vec.add(outPutLine);

//还要注意我们将相同的对象outPutLine添加到Vector eac时间

//(所以最后我们将有一个Vector的四个引用相同的

对象)

// [...]

}

k = 2;

// [等等......]

那么,它只是我还是有输出的时髦?例如,
元素S(0; 0)在哪里?!
I''m not sure if i''m going mad or stupid but as far as i can rely
on my eyes, when i ADD an element to a Vector, it gets put on
ALL the position, instead of the last one. It''s like if it rolls all
the way down the list leaving its footprints along the way.

I wrote, like:
import java.util.*;

public class Temp {
public static void main (String[] arg) {
Vector vec = new Vector ();
String[] outPutLine = new String[3];
Note that there is only one array outPutLine, and its value never changes:
it always refers to the same array of 3 String''s.
String line;
for (int k = 0; k < 4; k++) {
for (int i = 0; i < outPutLine.length; i++)
outPutLine[i] = "S(" + i + ";" + k + ")";
vec.add (outPutLine);

System.out.println ("outPutLine " + k + " is: \t\t" + outPutLine[0] +
"\t" + outPutLine[1] + "\t" + outPutLine[2]);
System.out.println ("The result is:");
Let''s unwrap the outer loop.
int k = 0;
for (int i = 0; i < outPutLine.length; i++)
outPutLine[i] = "S(" + i + ";" + k + ")";
vec.add (outPutLine);
// [...]
}
k = 1;
for (int i = 0; i < outPutLine.length; i++)
outPutLine[i] = "S(" + i + ";" + k + ")";
// Note that we are overwiting the elements of outPutLine from the previous
iteration
vec.add (outPutLine);
// Note also that we add the same object outPutLine to the Vector eac time
// (so at the end we will have a Vector of four references to the same
object)
// [...]
}
k = 2;
// [etc...]
So, is it just me or is there funky with the output? Where did the
element S(0;0) go, for instance?!




用S(0; 1)覆盖它。


建议:移动作业outPutLine = new String [3];"在

" for(int k ...)中环。这样你就可以创建四个String [3],

,如果你想存储12个单独的字符串,这很有用......


Mut ,


克里斯

-

Chris Gray ch *** @ kiffer.eunet.be

/ k /嵌入式Java解决方案



You overwrote it with S(0;1).

Suggestion: move the assignment "outPutLine = new String[3];" inside the
"for (int k ...)" loop. That way you will be creating four String[3]''s,
which is useful if you want to store 12 separate strings ...

Mut,

Chris
--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions


这篇关于java.util.Vector的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆