ArrayList problem

Discussion in 'Java' started by jacov, Aug 25, 2004.

  1. jacov

    jacov Guest

    HI, i have a problem with an ArrayList of data: this is a snippet of my
    problem:
    //code begin
    ArrayList data = new ArrayList;
    Vector _v = returnAVector();
    for (int j = 0; j < _v.size(); j++) {
    data.add(_v.elementAt(j));
    }
    //code end

    Simple....

    The problem is that the arrayList contains only the first element
    repetead as many times (actually the list size)
    Any hint is highly welcome
    Thanks

    Jacov
     
    jacov, Aug 25, 2004
    #1
    1. Advertising

  2. jacov wrote:

    > HI, i have a problem with an ArrayList of data: this is a snippet of my
    > problem:
    > //code begin
    > ArrayList data = new ArrayList;
    > Vector _v = returnAVector();
    > for (int j = 0; j < _v.size(); j++) {
    > data.add(_v.elementAt(j));
    > }
    > //code end


    First, the loop is unnecessary, replace it with data.addAll(_v);
    But why do you copy the data anyway? If you need an ArrayList, create
    one directly.

    Second, the error is not in the code you posted but in the method
    returnAVector(). Most likely you do not add different objects to the
    Vecor but instead add the same one many times.
     
    Michael Borgwardt, Aug 25, 2004
    #2
    1. Advertising

  3. jacov

    Guest

    On Wed, 25 Aug 2004 15:36:00 +0200, Michael Borgwardt <> wrote:

    >jacov wrote:
    >
    >> HI, i have a problem with an ArrayList of data: this is a snippet of my
    >> problem:
    >> //code begin
    >> ArrayList data = new ArrayList;
    >> Vector _v = returnAVector();
    >> for (int j = 0; j < _v.size(); j++) {
    >> data.add(_v.elementAt(j));
    >> }
    >> //code end

    >
    >First, the loop is unnecessary, replace it with data.addAll(_v);
    >But why do you copy the data anyway? If you need an ArrayList, create
    >one directly.
    >
    >Second, the error is not in the code you posted but in the method
    >returnAVector(). Most likely you do not add different objects to the
    >Vecor but instead add the same one many times.


    Yeah, that's my conclusion too.

    I created this snippet around the sample and (of course) it works fine :

    //code begin

    private void test()
    {
    ArrayList data = new ArrayList();
    Vector _v = returnAVector();
    for (int j = 0; j < _v.size(); j++)
    {
    data.add(_v.elementAt(j));
    }
    showResults(data);
    }

    private Vector returnAVector()
    {
    Vector v = new Vector();
    v.add("111");
    v.add("222");
    v.add("333");
    return v;
    }

    private void showResults(ArrayList a)
    {
    for (int j = 0; j < a.size(); j++)
    {
    System.err.println("result (" + j + ") : " + a.get(j));
    }
    }

    //code end

    Output :

    result (0) : 111
    result (1) : 222
    result (2) : 333
     
    , Aug 25, 2004
    #3
  4. scribbled the following:
    > On Wed, 25 Aug 2004 15:36:00 +0200, Michael Borgwardt <> wrote:
    >>jacov wrote:
    >>> HI, i have a problem with an ArrayList of data: this is a snippet of my
    >>> problem:
    >>> //code begin
    >>> ArrayList data = new ArrayList;
    >>> Vector _v = returnAVector();
    >>> for (int j = 0; j < _v.size(); j++) {
    >>> data.add(_v.elementAt(j));
    >>> }
    >>> //code end

    >>
    >>First, the loop is unnecessary, replace it with data.addAll(_v);
    >>But why do you copy the data anyway? If you need an ArrayList, create
    >>one directly.
    >>
    >>Second, the error is not in the code you posted but in the method
    >>returnAVector(). Most likely you do not add different objects to the
    >>Vecor but instead add the same one many times.


    > Yeah, that's my conclusion too.


    That is the only sensible diagnosis. I didn't even need to test the
    above code, merely reading it told me that the for loop was OK, the
    fault must be in the Vector itself.

    > I created this snippet around the sample and (of course) it works fine :


    > //code begin


    > private void test()
    > {
    > ArrayList data = new ArrayList();
    > Vector _v = returnAVector();
    > for (int j = 0; j < _v.size(); j++)
    > {
    > data.add(_v.elementAt(j));
    > }
    > showResults(data);
    > }


    > private Vector returnAVector()
    > {
    > Vector v = new Vector();
    > v.add("111");
    > v.add("222");
    > v.add("333");
    > return v;
    > }


    > private void showResults(ArrayList a)
    > {
    > for (int j = 0; j < a.size(); j++)
    > {
    > System.err.println("result (" + j + ") : " + a.get(j));
    > }
    > }


    > //code end


    > Output :


    > result (0) : 111
    > result (1) : 222
    > result (2) : 333


    To the OP: How are you creating your Vector? I think you have something
    like this:

    Vector v = new Vector();
    MyObject m = new MyObject();
    m.setInt(1);
    v.add(m);
    m.setInt(2);
    v.add(m);
    m.setInt(3);
    v.add(m);

    Unlike some other languages, the v.add(m) call does *NOT* make a full
    copy of m and insert it to the vector v, leaving the original m free
    for changing. Instead, it merely adds a reference to m to the vector v,
    and thus you end up with three different references to the same object.
    You would have to use m = new MyObject(); before *EACH* call to
    v.add(m).

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I am lying."
    - Anon
     
    Joona I Palaste, Aug 25, 2004
    #4
  5. Joona I Palaste <> coughed up the following:
    > scribbled the following:
    >> On Wed, 25 Aug 2004 15:36:00 +0200, Michael Borgwardt
    >> <> wrote:
    >>> jacov wrote:
    >>>> HI, i have a problem with an ArrayList of data: this is a snippet
    >>>> of my
    >>>> problem:
    >>>> //code begin
    >>>> ArrayList data = new ArrayList;
    >>>> Vector _v = returnAVector();
    >>>> for (int j = 0; j < _v.size(); j++) {
    >>>> data.add(_v.elementAt(j));
    >>>> }
    >>>> //code end
    >>>
    >>> First, the loop is unnecessary, replace it with data.addAll(_v);
    >>> But why do you copy the data anyway? If you need an ArrayList,
    >>> create
    >>> one directly.
    >>>
    >>> Second, the error is not in the code you posted but in the method
    >>> returnAVector(). Most likely you do not add different objects to the
    >>> Vecor but instead add the same one many times.

    >
    >> Yeah, that's my conclusion too.

    >
    > That is the only sensible diagnosis. I didn't even need to test the
    > above code, merely reading it told me that the for loop was OK, the
    > fault must be in the Vector itself.
    >
    >> I created this snippet around the sample and (of course) it works
    >> fine :

    >
    >> //code begin

    >
    >> private void test()
    >> {
    >> ArrayList data = new ArrayList();
    >> Vector _v = returnAVector();
    >> for (int j = 0; j < _v.size(); j++)
    >> {
    >> data.add(_v.elementAt(j));
    >> }
    >> showResults(data);
    >> }

    >
    >> private Vector returnAVector()
    >> {
    >> Vector v = new Vector();
    >> v.add("111");
    >> v.add("222");
    >> v.add("333");
    >> return v;
    >> }

    >
    >> private void showResults(ArrayList a)
    >> {
    >> for (int j = 0; j < a.size(); j++)
    >> {
    >> System.err.println("result (" + j + ") : " + a.get(j));
    >> }
    >> }

    >
    >> //code end

    >
    >> Output :

    >
    >> result (0) : 111
    >> result (1) : 222
    >> result (2) : 333

    >
    > To the OP: How are you creating your Vector? I think you have
    > something
    > like this:
    >
    > Vector v = new Vector();
    > MyObject m = new MyObject();
    > m.setInt(1);
    > v.add(m);
    > m.setInt(2);
    > v.add(m);
    > m.setInt(3);
    > v.add(m);
    >
    > Unlike some other languages, the v.add(m) call does *NOT* make a full
    > copy of m and insert it to the vector v, leaving the original m free
    > for changing. Instead, it merely adds a reference to m to the vector
    > v,
    > and thus you end up with three different references to the same
    > object.
    > You would have to use m = new MyObject(); before *EACH* call to
    > v.add(m).


    or implement Cloneable (yada yada) if there is some accumulative state
    associated with the mutator, which (if the OP was doing what you suggest)
    /might/ be a situation he has.


    --
    Onedoctortoanother:"Ifthisismyrectalthermometer,wherethehell'smypen???"
     
    Thomas G. Marshall, Aug 25, 2004
    #5
  6. jacov

    jacov Guest

    In the loop (that is necessary and inside another loop) i always create
    a different instance of an object, so the strange behaviour is curious.
    Yet, i tried toi return a list directly, but the problem was the same.
    I suspect that this is a promlem of synchronization of ArrayList.
    But i tried also with hashtable, without the expected result.
    Java is a mistery sometimes

    Thomas G. Marshall wrote:
    > Joona I Palaste <> coughed up the following:
    >
    >> scribbled the following:
    >>
    >>>On Wed, 25 Aug 2004 15:36:00 +0200, Michael Borgwardt
    >>><> wrote:
    >>>
    >>>>jacov wrote:
    >>>>
    >>>>>HI, i have a problem with an ArrayList of data: this is a snippet
    >>>>>of my
    >>>>>problem:
    >>>>>//code begin
    >>>>>ArrayList data = new ArrayList;
    >>>>>Vector _v = returnAVector();
    >>>>>for (int j = 0; j < _v.size(); j++) {
    >>>>> data.add(_v.elementAt(j));
    >>>>>}
    >>>>>//code end
    >>>>
    >>>>First, the loop is unnecessary, replace it with data.addAll(_v);
    >>>>But why do you copy the data anyway? If you need an ArrayList,
    >>>>create
    >>>>one directly.
    >>>>
    >>>>Second, the error is not in the code you posted but in the method
    >>>>returnAVector(). Most likely you do not add different objects to the
    >>>>Vecor but instead add the same one many times.

    >>
    >>>Yeah, that's my conclusion too.

    >>
    >>That is the only sensible diagnosis. I didn't even need to test the
    >>above code, merely reading it told me that the for loop was OK, the
    >>fault must be in the Vector itself.
    >>
    >>
    >>>I created this snippet around the sample and (of course) it works
    >>>fine :

    >>
    >>>//code begin

    >>
    >>>private void test()
    >>>{
    >>>ArrayList data = new ArrayList();
    >>>Vector _v = returnAVector();
    >>>for (int j = 0; j < _v.size(); j++)
    >>>{
    >>>data.add(_v.elementAt(j));
    >>>}
    >>>showResults(data);
    >>>}

    >>
    >>>private Vector returnAVector()
    >>>{
    >>>Vector v = new Vector();
    >>>v.add("111");
    >>>v.add("222");
    >>>v.add("333");
    >>>return v;
    >>>}

    >>
    >>>private void showResults(ArrayList a)
    >>>{
    >>>for (int j = 0; j < a.size(); j++)
    >>>{
    >>>System.err.println("result (" + j + ") : " + a.get(j));
    >>>}
    >>>}

    >>
    >>>//code end

    >>
    >>>Output :

    >>
    >>> result (0) : 111
    >>> result (1) : 222
    >>> result (2) : 333

    >>
    >>To the OP: How are you creating your Vector? I think you have
    >>something
    >>like this:
    >>
    >>Vector v = new Vector();
    >>MyObject m = new MyObject();
    >>m.setInt(1);
    >>v.add(m);
    >>m.setInt(2);
    >>v.add(m);
    >>m.setInt(3);
    >>v.add(m);
    >>
    >>Unlike some other languages, the v.add(m) call does *NOT* make a full
    >>copy of m and insert it to the vector v, leaving the original m free
    >>for changing. Instead, it merely adds a reference to m to the vector
    >>v,
    >>and thus you end up with three different references to the same
    >>object.
    >>You would have to use m = new MyObject(); before *EACH* call to
    >>v.add(m).

    >
    >
    > or implement Cloneable (yada yada) if there is some accumulative state
    > associated with the mutator, which (if the OP was doing what you suggest)
    > /might/ be a situation he has.
    >
    >
     
    jacov, Aug 26, 2004
    #6
  7. jacov <> scribbled the following:
    > In the loop (that is necessary and inside another loop) i always create
    > a different instance of an object, so the strange behaviour is curious.
    > Yet, i tried toi return a list directly, but the problem was the same.
    > I suspect that this is a promlem of synchronization of ArrayList.
    > But i tried also with hashtable, without the expected result.
    > Java is a mistery sometimes


    Then the problem must lie in some other part of your code. Now comes the
    time when I have to say:
    POST YOUR CODE!
    I can diagnose your problem much better when I can see it, instead of
    having to try to read your mind.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
    - ALF
     
    Joona I Palaste, Aug 26, 2004
    #7
  8. jacov wrote:

    > In the loop (that is necessary and inside another loop) i always create
    > a different instance of an object, so the strange behaviour is curious.


    The other common misunderstanding that causes this kind of problem is
    failure to understand that Java uses pass-by-value, so an assignment
    to a method parameter inside the method has no effect outside.

    > Yet, i tried toi return a list directly, but the problem was the same.


    Yes, we told you that the problem lies not with the copying (which is
    just a pointless waste of time) but with the way the list, vector,
    or whatever is built.

    > I suspect that this is a promlem of synchronization of ArrayList.


    You suspect wrong.
     
    Michael Borgwardt, Aug 26, 2004
    #8
  9. Michael Borgwardt <> coughed up the
    following:
    > jacov wrote:
    >
    >> In the loop (that is necessary and inside another loop) i always
    >> create a different instance of an object, so the strange behaviour
    >> is curious.

    >
    > The other common misunderstanding that causes this kind of problem is
    > failure to understand that Java uses pass-by-value,


    Oh so good to meet another of the PBV brethren...(see the recent wars I've
    had to fight in this regard in c.programming, and c.object, and c.smalltalk
    (cross posted {author runs for cover} ). Such wars seem commonplace these
    days, so nothing new:

    http://groups.google.com/groups?q=g...selm=o9CEc.23061$

    (I'll have to cop to the fact that jon skeet was the one who clarified the
    notion for /me/ oh so long ago...someone's gonna have to hold a gun to his
    head and bring him back to c.l.j.*)

    FWIW, I'll bet (not much) that you've nailed the issue of the OP.


    ....[stomp]...


    --
    It'salwaysbeenmygoalinlifetocreateasignaturethatendedwiththeword"blarphoogy"
    ..
     
    Thomas G. Marshall, Aug 26, 2004
    #9
  10. Thomas G. Marshall <> scribbled the following:
    > Michael Borgwardt <> coughed up the
    > following:
    >> jacov wrote:
    >>
    >>> In the loop (that is necessary and inside another loop) i always
    >>> create a different instance of an object, so the strange behaviour
    >>> is curious.

    >>
    >> The other common misunderstanding that causes this kind of problem is
    >> failure to understand that Java uses pass-by-value,


    > Oh so good to meet another of the PBV brethren...(see the recent wars I've
    > had to fight in this regard in c.programming, and c.object, and c.smalltalk
    > (cross posted {author runs for cover} ). Such wars seem commonplace these
    > days, so nothing new:


    (snip)

    "PBV brethren"? I thought it was the established opinion of the entire
    regularship (can I say that?) of this newsgroup that Java always passes
    by value. There is no need for "PBV brethren", on the contrary, it is
    more likely that there comes some sort of "PBR resistance". =)

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "How can we possibly use sex to get what we want? Sex IS what we want."
    - Dr. Frasier Crane
     
    Joona I Palaste, Aug 26, 2004
    #10
  11. Joona I Palaste <> coughed up the following:
    > Thomas G. Marshall
    > <> scribbled the
    > following:
    >> Michael Borgwardt <> coughed up the
    >> following:
    >>> jacov wrote:
    >>>
    >>>> In the loop (that is necessary and inside another loop) i always
    >>>> create a different instance of an object, so the strange behaviour
    >>>> is curious.
    >>>
    >>> The other common misunderstanding that causes this kind of problem
    >>> is
    >>> failure to understand that Java uses pass-by-value,

    >
    >> Oh so good to meet another of the PBV brethren...(see the recent
    >> wars I've
    >> had to fight in this regard in c.programming, and c.object, and
    >> c.smalltalk (cross posted {author runs for cover} ). Such wars seem
    >> commonplace these
    >> days, so nothing new:

    >
    > (snip)
    >
    > "PBV brethren"? I thought it was the established opinion of the entire
    > regularship (can I say that?) of this newsgroup that Java always
    > passes
    > by value. There is no need for "PBV brethren", on the contrary, it is
    > more likely that there comes some sort of "PBR resistance". =)


    Yes, you're right. I'm just a little battle-scarred from saying this in
    other ng's...


    --
    "It's easier to be terrified by an enemy you admire."
    -Thufir Hawat, Mentat and Master of Assassins to House Atreides
     
    Thomas G. Marshall, Aug 26, 2004
    #11
  12. jacov

    Chris Uppal Guest

    Thomas G. Marshall wrote:

    > see the recent wars I've
    > had to fight in this regard in c.programming, and c.object, and
    > c.smalltalk (cross posted {author runs for cover} ). Such wars seem
    > commonplace these days, so nothing new:


    and:

    > Yes, you're right. I'm just a little battle-scarred from saying this in
    > other ng's...


    I can see how you might be feeling a little jaded. Yet you have, apparently
    voluntarily, just introduced the subject of curly braces /right in the middle/
    of the dynamic-typing mega-thread !

    Beat's me. You must be a glutton for punishment...

    ;-)

    -- chris

    P.S. I also see Java as solely pass-by-value, but it'd be a hell of sight
    easier to explain these matters if we weren't so coy about the word "pointer".
     
    Chris Uppal, Aug 26, 2004
    #12
  13. Chris Uppal <-this.org> scribbled the following:
    > Thomas G. Marshall wrote:
    >> see the recent wars I've
    >> had to fight in this regard in c.programming, and c.object, and
    >> c.smalltalk (cross posted {author runs for cover} ). Such wars seem
    >> commonplace these days, so nothing new:


    > and:


    >> Yes, you're right. I'm just a little battle-scarred from saying this in
    >> other ng's...


    > I can see how you might be feeling a little jaded. Yet you have, apparently
    > voluntarily, just introduced the subject of curly braces /right in the middle/
    > of the dynamic-typing mega-thread !


    Should he have used square brackets instead?

    > P.S. I also see Java as solely pass-by-value, but it'd be a hell of sight
    > easier to explain these matters if we weren't so coy about the word "pointer".


    I agree. Java calls them "references", but what they really act like is
    pointers without the pointer arithmetic. (Opaque pointers?) But when a C
    or C++ programmer sees the word "reference", he/she immediately
    equivocates it with the C++ reference concept, and thinks it works the
    same way, just like the nuts you tighten on metal bolts and the nuts you
    eat as a slight snack are the same thing.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "B-but Angus! You're a dragon!"
    - Mickey Mouse
     
    Joona I Palaste, Aug 26, 2004
    #13
  14. Joona I Palaste <> coughed up the following:
    > Chris Uppal <-this.org> scribbled the
    > following:
    >> Thomas G. Marshall wrote:
    >>> see the recent wars I've
    >>> had to fight in this regard in c.programming, and c.object, and
    >>> c.smalltalk (cross posted {author runs for cover} ). Such wars seem
    >>> commonplace these days, so nothing new:

    >
    >> and:

    >
    >>> Yes, you're right. I'm just a little battle-scarred from saying
    >>> this in other ng's...

    >
    >> I can see how you might be feeling a little jaded. Yet you have,
    >> apparently voluntarily, just introduced the subject of curly braces
    >> /right in the middle/ of the dynamic-typing mega-thread !

    >
    > Should he have used square brackets instead?
    >
    >> P.S. I also see Java as solely pass-by-value, but it'd be a hell of
    >> sight easier to explain these matters if we weren't so coy about the
    >> word "pointer".

    >
    > I agree. Java calls them "references", but what they really act like
    > is pointers without the pointer arithmetic. (Opaque pointers?) But
    > when a C
    > or C++ programmer sees the word "reference", he/she immediately
    > equivocates it with the C++ reference concept, and thinks it works the
    > same way, just like the nuts you tighten on metal bolts and the nuts
    > you
    > eat as a slight snack are the same thing.


    Well, that last metaphor is a stretch :), but I have to agree.

    They should have called them pointers, even though "references" is actually
    technically accurate.

    What's interesting, and is still without explanation, is the
    /NullPointerException/ exception. As Ali G might say, wha's up wit dat? It
    deserz no respek.





    --
    "It's easier to be terrified by an enemy you admire."
    -Thufir Hawat, Mentat and Master of Assassins to House Atreides
     
    Thomas G. Marshall, Aug 26, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,794
    Natty Gur
    Aug 19, 2003
  2. VB Programmer

    Problem: Late binding error with arraylist

    VB Programmer, Jun 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    437
    John Saunders
    Jun 18, 2004
  3. Kaidi
    Replies:
    4
    Views:
    2,486
    Kaidi
    Jan 3, 2004
  4. xz
    Replies:
    16
    Views:
    2,441
  5. Philipp
    Replies:
    6
    Views:
    971
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page