Results 1 to 7 of 7
- 03-22-2011, 05:05 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
ArrayList Keeps Repeating The Same Object
Uggh. I am a noob. And I am also stuck. I am an old-timer C command-line programmer and I am trying to wrap my brain around OOP.
Here's the skinny. I am trying to create a dynamic array of objects. That is, I have no idea in advance how many records of data I am going to process. So, reading up on this, I think an ArrayList seems like the way to go.
I have an object called oneCandle. (Think stockchart candlestick), who's code looks like this:
It basically acts like a record in C.public class oneCandle {
private double opr, hpr, lpr, cpr, vol;
private String candleDate, candleDay;
public oneCandle() {
}
// getters and setters are listed here...omitted for brevity
}
Then I operate on it with a method called fetchCandles (in FetchCandleFile )here.
Here, again for brevity, I will just show the method.
I am trying to print the fields of each object in candleChain as a debug method but ALL the fields in candleChain have the EXACT same object in them. And it's the last object added.public static ArrayList fetchCandles() throws IOException {
// String openP, closeP, lowP, highP, volP;
String junkme;
int i = 0;
try {
file = new File(filename);
Scanner input = new Scanner(file);
// Go to the file and strip out the data until has.Next == False
while (input.hasNext()) {
i++;
tempCandle.setCandleDay(input.next());
tempCandle.setCandleDate(input.next());
tempCandle.setOpr(Double.valueOf(input.next()).dou bleValue());
tempCandle.setHpr(Double.valueOf(input.next()).dou bleValue());
tempCandle.setLpr(Double.valueOf(input.next()).dou bleValue());
tempCandle.setCpr(Double.valueOf(input.next()).dou bleValue());
tempCandle.setVol(Double.valueOf(input.next()).dou bleValue());
// add the object to ArrayList "candleChain"
candleChain.add(tempCandle);
}
} catch (FileNotFoundException e) {
System.err.format("File is not found");
}
// -------- DEBUG CODE. print the objects from candleChain to the screen.
for (int j = 0; j < candleChain.size(); j++) {
oneCandle temp2;
temp2 = (oneCandle) candleChain.get(j);
System.out.println(temp2.getCandleDay() + "\n");
System.out.println("The Day is :" + temp2.getCandleDay() + "\n");
System.out.println("The Date is :" + temp2.getCandleDate() + "\n");
System.out.println("The Open is :" + temp2.getOpr() + "\n");
System.out.println("The High is :" + temp2.getHpr() + "\n");
System.out.println("The Low is :" + temp2.getLpr() + "\n");
System.out.println("The Close is :" + temp2.getCpr() + "\n");
System.out.println("The Volume is :" + temp2.getVol() + "\n");
}
//=====================
return candleChain;
}
What gives? That is, what am I doing wrong and how do I correct this?
Thanks in advance.
- 03-22-2011, 05:35 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
Your code is adding the same object to the ArrayList every time. To fix this, you need to create a new instance of oneCandle each time. See the highlighted code below:
Java Code:while (input.hasNext()) { i++; [B][COLOR="Blue"]tempCandle = new oneCandle ();[/COLOR][/B] tempCandle.setCandleDay(input.next()); tempCandle.setCandleDate(input.next()); tempCandle.setOpr(Double.valueOf(input.next()).dou bleValue()); tempCandle.setHpr(Double.valueOf(input.next()).dou bleValue()); tempCandle.setLpr(Double.valueOf(input.next()).dou bleValue()); tempCandle.setCpr(Double.valueOf(input.next()).dou bleValue()); tempCandle.setVol(Double.valueOf(input.next()).dou bleValue()); // add the object to ArrayList "candleChain" candleChain.add(tempCandle); }Last edited by FlipPoker@gmail.com; 03-22-2011 at 05:43 PM.
- 03-22-2011, 05:40 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Rather than just handing out code you might want to explain why that is so. Don't you think that is just a little more valuable than code that the OP won't really recognise how it is different from his.
- 03-22-2011, 05:56 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Flip poker. Thank you!
masijade. That's a awfully good question. Why do I have to create another instance? Can't I just write over the old one over and over and save extra memory?
*confused*
- 03-22-2011, 06:23 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
See?
It's because what you are adding to the arraylist is a reference to an already existing object. Which means you then have two references to the same object. The one you just added to the list and the one you still have in method from which you added it. Note, two (and more once you add more) refereences to the same object, so when you make changes to the object using one of those references, you also see those effects under the other reference(s), again, because they are the same object. Once you do "new" you are assigning a new reference to the variable, thereby the reference in the list points to a different object than the one in the method that added it to the list.
- 03-22-2011, 06:52 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
It's pretty much the same thing as pointers in C.
Changing the value through pointer pa will be reflected in pb as well, and vice versa. The Java equivalent is this:Java Code:int a = 5; int *pa = &a; int *pb = &a;
Both o and r reference the same object, if you change r, o will also be changed (since you are dealing with just 1 object, referenced by 2 variables).Java Code:Object o = new Object(); Object r = o;
Ever seen a dog chase its tail? Now that's an infinite loop.
- 03-22-2011, 08:44 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Got it. Thanks. Once you used the C pointer analogy it made complete sense.
Basically, every element in my ArrayList is pointing to the same object, which just happens to be the first and only object that I created.
Well, I added the line of code in the while list (changed to a do-while btw) and it works perfectly.
Again, thanks guys.
Similar Threads
-
Getting value of Object[] in an ArrayList
By adela in forum New To JavaReplies: 4Last Post: 01-28-2011, 08:13 AM -
A private static ArrayList hold an object and static getter ArrayList
By Louis in forum New To JavaReplies: 2Last Post: 11-16-2010, 05:51 PM -
add object to ArrayList (object is from extends other class)
By mBull in forum Java AppletsReplies: 3Last Post: 03-15-2010, 08:44 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
how to return an object from an arraylist
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 06:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks