Strange behavior of a java.util.Vector

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Konrad Den Ende

    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.leng th; i++)
    outPutLine[i] = "S(" + i + ";" + k + ")";
    vec.add (outPutLine);

    System.out.prin tln ("outPutLine " + k + " is: \t\t" + outPutLine[0] +
    "\t" + outPutLine[1] + "\t" + outPutLine[2]);
    System.out.prin tln ("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.prin tln (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
    ---------------------------------------------------




  • Jan van Mansum

    #2
    Re: Strange behavior of a java.util.Vecto r

    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:
    [color=blue]
    > 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.leng th; i++)
    > outPutLine[i] = "S(" + i + ";" + k + ")";
    > vec.add (outPutLine);
    >
    > System.out.prin tln ("outPutLine " + k + " is: \t\t" + outPutLine[0] +
    > "\t" + outPutLine[1] + "\t" + outPutLine[2]);
    > System.out.prin tln ("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.prin tln (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?!
    >[/color]

    Comment

    • Jan van Mansum

      #3
      Re: Strange behavior of a java.util.Vecto r

      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:
      [color=blue]
      > 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.leng th; i++)
      > outPutLine[i] = "S(" + i + ";" + k + ")";
      > vec.add (outPutLine);
      >
      > System.out.prin tln ("outPutLine " + k + " is: \t\t" + outPutLine[0] +
      > "\t" + outPutLine[1] + "\t" + outPutLine[2]);
      > System.out.prin tln ("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.prin tln (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?!
      >[/color]

      Comment

      • chris

        #4
        Re: Strange behavior of a java.util.Vecto r

        Konrad Den Ende wrote:
        [color=blue]
        > 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];[/color]

        Note that there is only one array outPutLine, and its value never changes:
        it always refers to the same array of 3 String's.
        [color=blue]
        > String line;
        > for (int k = 0; k < 4; k++) {
        > for (int i = 0; i < outPutLine.leng th; i++)
        > outPutLine[i] = "S(" + i + ";" + k + ")";
        > vec.add (outPutLine);
        >
        > System.out.prin tln ("outPutLine " + k + " is: \t\t" + outPutLine[0] +
        > "\t" + outPutLine[1] + "\t" + outPutLine[2]);
        > System.out.prin tln ("The result is:");[/color]

        Let's unwrap the outer loop.
        int k = 0;
        for (int i = 0; i < outPutLine.leng th; i++)
        outPutLine[i] = "S(" + i + ";" + k + ")";
        vec.add (outPutLine);
        // [...]
        }
        k = 1;
        for (int i = 0; i < outPutLine.leng th; 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...]
        [color=blue]
        > So, is it just me or is there funky with the output? Where did the
        > element S(0;0) go, for instance?![/color]

        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 chris@kiffer.eu net.be
        /k/ Embedded Java Solutions

        Comment

        • chris

          #5
          Re: Strange behavior of a java.util.Vecto r

          Konrad Den Ende wrote:
          [color=blue]
          > 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];[/color]

          Note that there is only one array outPutLine, and its value never changes:
          it always refers to the same array of 3 String's.
          [color=blue]
          > String line;
          > for (int k = 0; k < 4; k++) {
          > for (int i = 0; i < outPutLine.leng th; i++)
          > outPutLine[i] = "S(" + i + ";" + k + ")";
          > vec.add (outPutLine);
          >
          > System.out.prin tln ("outPutLine " + k + " is: \t\t" + outPutLine[0] +
          > "\t" + outPutLine[1] + "\t" + outPutLine[2]);
          > System.out.prin tln ("The result is:");[/color]

          Let's unwrap the outer loop.
          int k = 0;
          for (int i = 0; i < outPutLine.leng th; i++)
          outPutLine[i] = "S(" + i + ";" + k + ")";
          vec.add (outPutLine);
          // [...]
          }
          k = 1;
          for (int i = 0; i < outPutLine.leng th; 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...]
          [color=blue]
          > So, is it just me or is there funky with the output? Where did the
          > element S(0;0) go, for instance?![/color]

          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 chris@kiffer.eu net.be
          /k/ Embedded Java Solutions

          Comment

          • Konrad Den Ende

            #6
            Re: Strange behavior of a java.util.Vecto r

            > Remember that in Java all objects and arrays are actually references[color=blue]
            > (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.[/color]

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




            Comment

            • Konrad Den Ende

              #7
              Re: Strange behavior of a java.util.Vecto r

              > Remember that in Java all objects and arrays are actually references[color=blue]
              > (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.[/color]

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




              Comment

              • Konrad Den Ende

                #8
                Re: Strange behavior of a java.util.Vecto r

                > Suggestion: move the assignment "outPutLine = new String[3];" inside the[color=blue]
                > "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 ...[/color]

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




                Comment

                • Konrad Den Ende

                  #9
                  Re: Strange behavior of a java.util.Vecto r

                  > Suggestion: move the assignment "outPutLine = new String[3];" inside the[color=blue]
                  > "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 ...[/color]

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




                  Comment

                  • hiwa

                    #10
                    Re: Strange behavior of a java.util.Vecto r

                    Please try putting CORRECT brace pairs around all of your for loops.

                    Comment

                    • hiwa

                      #11
                      Re: Strange behavior of a java.util.Vecto r

                      Please try putting CORRECT brace pairs around all of your for loops.

                      Comment

                      • Konrad Den Ende

                        #12
                        Re: Strange behavior of a java.util.Vecto r

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




                        Comment

                        • Konrad Den Ende

                          #13
                          Re: Strange behavior of a java.util.Vecto r

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




                          Comment

                          • chris

                            #14
                            Re: Strange behavior of a java.util.Vecto r

                            Konrad Den Ende wrote:
                            [color=blue][color=green]
                            >> 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 ...[/color]
                            >
                            > 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.[/color]

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

                            --
                            Chris Gray chris@kiffer.eu net.be
                            /k/ Embedded Java Solutions

                            Comment

                            • chris

                              #15
                              Re: Strange behavior of a java.util.Vecto r

                              Konrad Den Ende wrote:
                              [color=blue][color=green]
                              >> 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 ...[/color]
                              >
                              > 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.[/color]

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

                              --
                              Chris Gray chris@kiffer.eu net.be
                              /k/ Embedded Java Solutions

                              Comment

                              Working...