By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

Strange behavior of a java.util.Vector

P: n/a
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
---------------------------------------------------


Jul 17 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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:
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?!

Jul 17 '05 #2

P: n/a
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:
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?!

Jul 17 '05 #3

P: n/a
Konrad Den Ende wrote:
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?!


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

Jul 17 '05 #4

P: n/a
Konrad Den Ende wrote:
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?!


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

Jul 17 '05 #5

P: n/a
> 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.


Ah, that's better. Now i know i'm not crazy (at least not because of
that, hehe). How do i solve this problem, then?

I can think of a way or two, but perhaps if there's a neater way of
doing that, i'll be spared from too much thinking... :)

--

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
---------------------------------------------------


Jul 17 '05 #6

P: n/a
> 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.


Ah, that's better. Now i know i'm not crazy (at least not because of
that, hehe). How do i solve this problem, then?

I can think of a way or two, but perhaps if there's a neater way of
doing that, i'll be spared from too much thinking... :)

--

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
---------------------------------------------------


Jul 17 '05 #7

P: n/a
> 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 ...


Thank you Chris. I'll do that.

It's funny because i've just done reading Jans answer and asked about
a neat way of solving the issue (since he limited himself to merely asnwer
my original question, thus not spoiling the fun of "killing my own food").
Than i updated the view and there was your hint.

Getting answered before asking - is there a better service than so?
Once again, thanks!

--

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
---------------------------------------------------


Jul 17 '05 #8

P: n/a
> 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 ...


Thank you Chris. I'll do that.

It's funny because i've just done reading Jans answer and asked about
a neat way of solving the issue (since he limited himself to merely asnwer
my original question, thus not spoiling the fun of "killing my own food").
Than i updated the view and there was your hint.

Getting answered before asking - is there a better service than so?
Once again, thanks!

--

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
---------------------------------------------------


Jul 17 '05 #9

P: n/a
Please try putting CORRECT brace pairs around all of your for loops.
Jul 17 '05 #10

P: n/a
Please try putting CORRECT brace pairs around all of your for loops.
Jul 17 '05 #11

P: n/a
> Please try putting CORRECT brace pairs around all of your for loops.

Define "correct brace pairs", please. Did you mean the fact that i
put "{" at the end of the row?
--

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
---------------------------------------------------


Jul 17 '05 #12

P: n/a
> Please try putting CORRECT brace pairs around all of your for loops.

Define "correct brace pairs", please. Did you mean the fact that i
put "{" at the end of the row?
--

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
---------------------------------------------------


Jul 17 '05 #13

P: n/a
Konrad Den Ende wrote:
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 ...


Thank you Chris. I'll do that.

It's funny because i've just done reading Jans answer and asked about
a neat way of solving the issue (since he limited himself to merely asnwer
my original question, thus not spoiling the fun of "killing my own food").
Than i updated the view and there was your hint.


When I saw jan's answer I knew I had said too much. :)

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

Jul 17 '05 #14

P: n/a
Konrad Den Ende wrote:
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 ...


Thank you Chris. I'll do that.

It's funny because i've just done reading Jans answer and asked about
a neat way of solving the issue (since he limited himself to merely asnwer
my original question, thus not spoiling the fun of "killing my own food").
Than i updated the view and there was your hint.


When I saw jan's answer I knew I had said too much. :)

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

Jul 17 '05 #15

P: n/a
"Konrad Den Ende" <ch************@bigfoot.com> wrote in message news:<bv**********@news.gu.se>...
Please try putting CORRECT brace pairs around all of your for loops.


Define "correct brace pairs", please. Did you mean the fact that i
put "{" at the end of the row?
--

Kindly
Konrad


Oh, I mean 'logically correct.'

Expand|Select|Wrap|Line Numbers
  1. this_for_loop( ..... ){
  2. does_this;
  3. and_does_this;
  4. and_also_does_this;
  5. }
  6. but_never_does_this;
  7.  
Even when the loop has only one line content, it should be
enclosed in a brace pair for better readability.
Jul 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.