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; }
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.
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.
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:
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
posted
0
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.
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);
Perfect! Thanks Brian that's just what I was looking for.
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
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.
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);
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.