Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Best way for adding object to ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best way for adding object to ArrayList" Watch "Best way for adding object to ArrayList" New topic
Author

Best way for adding object to ArrayList

Jim Bauer
Greenhorn

Joined: Nov 07, 2005
Posts: 9
I'm looking for a little feed back. Is this truly the best way to add objects to an ArrayList?

import java.util.ArrayList;

public class Feeder {
private String pcode;
private String pdata;
private int pseverity;


public Feeder() {
}

public static void main(String[] args) {
Feeder feedback = new Feeder();
ArrayList feedbacklist = new ArrayList();

feedbacklist.add(feedback = new Feeder());
feedback.setcode("FST5678");
feedback.setdata("Must be transfered to Customer Relations");
feedback.setseverity(3);
int f = feedbacklist.size();
feedbacklist.set((f-1), feedback);

feedbacklist.add(feedback = new Feeder());
feedback.setcode("FSR1234");
feedback.setdata("Transfer to Help Desk");
feedback.setseverity(1);
f = feedbacklist.size();
feedbacklist.set((f-1), feedback);

}

public void setcode(String code){
pcode = code;
}

public String getcode(){
return pcode;
}

public void setdata(String data){
pdata = data;
}

public String getdata(){
return pdata;
}

public void setseverity(int severity){
pseverity = severity;
}

public int getseverity(){
return pseverity;
}

}
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

Umm, yes/no?

What your doing is valid, but most people don't usually initialize an object while adding it to array so:



Although, there's nothing wrong with either, the second is just easier to read and maintain. As for the end of each block "feedbacklist.set((f-1), feedback);", this is redundent code. The item is all ready set at that particular block, so calling it actually does nothing.

I think the confusion might be that you think once you add an object to an array you've lost the ability to modify it directly. This is far from the truth. You can put the same object in 50 different arrays and as long as you have a reference to the original object, any modification to it will affect the version in all 50 arrays. The arrays contain pointers to the object, they don't contain copies of the object.


My Blog: Down Home Country Coding with Scott Selikoff
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
The call to ArrayList's set() is redundant, since you just added that element!
As far as style goes, it makes more sense to set all the attributes of
the element before adding it, generally speaking. That way, if a runtime
error occurs while setting the element's attributes, you won't have screwed
up the state of the list with a half-built object. If that is what you want, of course.


There is no emoticon for what I am feeling!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, I don't think it is the best way. The first part of your code does add an element to an ArrayList (although it's hard to understand the way you write it), but then you take that element and replace it by itself (which is unnecessary). Here's how I would do it:
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

One thing I forgot to mention, although you can modify an object in an array using its original pointer, you cannot modify the arrays pointers to it in any way, so there is one limitation.

For example:

Jim Bauer
Greenhorn

Joined: Nov 07, 2005
Posts: 9
I guess the real problem I am haveing is adding the next element using the feedback object. If I change value in feedback it then changes every element in the array.

If I write my code this way:

feedback.setcode("FST5678");
feedback.setdata("Must be transfered to Customer Relations");
feedback.setseverity(3);
feedbacklist.add(feedback);

feedback.setcode("FSR1234");
feedback.setdata("Transfer to Help Desk");
feedback.setseverity(1);
feedbacklist.add(feedback);

every time I set feed back it changes every element in my ArrayList.
Sorry I'm not making myself real clear, but I don't know how else to state it.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
so write it this way:

feedback = new Feeder();
feedback.setcode("FST5678");
feedback.setdata("Must be transfered to Customer Relations");
feedback.setseverity(3);
feedbacklist.add(feedback);

feedback = new Feeder();
feedback.setcode("FSR1234");
feedback.setdata("Transfer to Help Desk");
feedback.setseverity(1);
feedbacklist.add(feedback);


bitguru blog
Jim Bauer
Greenhorn

Joined: Nov 07, 2005
Posts: 9
Perfect!
Thanks Brian that's just what I was looking for.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
That's a common mistake, so it's worth pointing it out: when you add
an object to a collection, what's being added is a reference to the exisiting
object, *not* a copy of the object. If you want to add multiple objects
be sure to create multiple objects to begin with -- the collection doesn't
create the copies for you.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

For completeness, this is also perfectly valid:

feedback = new Feeder();
feedbacklist.add(feedback);
feedback.setcode("FST5678");
feedback.setdata("Must be transfered to Customer Relations");
feedback.setseverity(3);

feedback = new Feeder();
feedbacklist.add(feedback);
feedback.setcode("FSR1234");
feedback.setdata("Transfer to Help Desk");
feedback.setseverity(1);
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Best way for adding object to ArrayList
 
Similar Threads
Color in JTable
Simple ArrayList conundrum?
use of static methods and variables
help using static methods
Inheritance doubt