Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ArrayList problem

Reply
Thread Tools

ArrayList problem

 
 
jacov
Guest
Posts: n/a
 
      08-25-2004
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


 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      08-25-2004
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.
 
Reply With Quote
 
 
 
 
brandon@hotmall.com
Guest
Posts: n/a
 
      08-25-2004
On Wed, 25 Aug 2004 15:36:00 +0200, Michael Borgwardt <(E-Mail Removed)> 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
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      08-25-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) scribbled the following:
> On Wed, 25 Aug 2004 15:36:00 +0200, Michael Borgwardt <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am lying."
- Anon
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-25-2004
Joona I Palaste <(E-Mail Removed)> coughed up the following:
> (E-Mail Removed) scribbled the following:
>> On Wed, 25 Aug 2004 15:36:00 +0200, Michael Borgwardt
>> <(E-Mail Removed)> 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,wh erethehell'smypen???"



 
Reply With Quote
 
jacov
Guest
Posts: n/a
 
      08-26-2004
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 <(E-Mail Removed)> coughed up the following:
>
>>(E-Mail Removed) scribbled the following:
>>
>>>On Wed, 25 Aug 2004 15:36:00 +0200, Michael Borgwardt
>>><(E-Mail Removed)> 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.
>
>


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      08-26-2004
jacov <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
- ALF
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      08-26-2004
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.
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-26-2004
Michael Borgwardt <(E-Mail Removed)> 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:...03.gnilink.net

(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'salwaysbeenmygoalinlifetocreateasignaturethaten dedwiththeword"blarphoogy"
..


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      08-26-2004
Thomas G. Marshall <(E-Mail Removed). com> scribbled the following:
> Michael Borgwardt <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clear Arraylist vs new ArrayList Philipp Java 6 05-28-2008 09:07 PM
Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList? xz Java 16 08-04-2007 10:33 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments