Results 1 to 7 of 7
  1. #1
    Morpheus is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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:

    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

    }
    It basically acts like a record in C.

    Then I operate on it with a method called fetchCandles (in FetchCandleFile )here.

    Here, again for brevity, I will just show the method.

    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;
    }
    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.

    What gives? That is, what am I doing wrong and how do I correct this?

    Thanks in advance.

  2. #2
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    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.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    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.

  4. #4
    Morpheus is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    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*

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    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.

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    It's pretty much the same thing as pointers in C.
    Java Code:
    int a = 5;
    int *pa = &a;
    int *pb = &a;
    Changing the value through pointer pa will be reflected in pb as well, and vice versa. The Java equivalent is this:
    Java Code:
    Object o = new Object();
    Object r = o;
    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).
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    Morpheus is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    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

  1. Getting value of Object[] in an ArrayList
    By adela in forum New To Java
    Replies: 4
    Last Post: 01-28-2011, 08:13 AM
  2. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  3. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •