473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Strange behavior of a java.util.Vector

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
15 2429
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
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
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
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
> 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
> 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
> 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
> 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
Please try putting CORRECT brace pairs around all of your for loops.
Jul 17 '05 #10
Please try putting CORRECT brace pairs around all of your for loops.
Jul 17 '05 #11
> 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
> 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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Martin | last post by:
Hi All, I am a relative newbie to Java / Applets, but am despirately after some help ! I have got the following code, which is basically a listing with button items along the sides, allowing...
4
by: angel | last post by:
A java runtime environment includes jvm and java class (for example classes.zip in sun jre). Of course jython need jvm,but does it need java class. Thanx
5
by: Egghead | last post by:
Hi, Does anyone know if there is any C#'s class equivalent to java.utl.Vector? I think the System.Collections.ArrayList is not the same: In java: for(int i = 0; i<myVector.length;i++) {...
0
by: snkssa | last post by:
Hi, I got the following error while building an application with ant. please can you give the solution or reason why it is failing? Thank you create_zip_linux: ...
5
by: comp.lang.php | last post by:
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA $_POST if ($_POST && (!is_array($leaseObj->errorArray)...
0
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build...
1
by: amgupta8 | last post by:
Note: This problem occurred when I updated the JDK from 1.3.1 to 1.4.1 or 1.4.2. Nothing else was changed in the code, other than updating the JDK on the database server (dbm cfg parm jdk_path) and...
2
by: minus | last post by:
public class Test { public static void main (String arg) { // "012345678910" alignment guide String results = {"AR 4 BR 5", "CR 0 DR 1",...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.