Data is being overridden strangely when I append a new object to an array of the same object type in Java

Viewed 81

So I have two objects. One titled 'drillingRecords' has two arrays for storing parsed line information from a file. The second titled 'drillingArray' contains an array to store 'drillingRecord' objects. The goal is to store one 'drillingArray' object within a 'drillingArray[]' for each line of data within the file. (I know they should be capitalized I'm just following the instructions given to me)

I am reading through the file fine but a weird problem is occurring where the array is outputting the first line once, second line twice, and so on when iterated within the file reading while loop. The array appears to be overwriting the previous line's data. I believe this because once I tried to output the data outside of the while loop it outputs 10 lines containing the last line of file data.

Here is the file reading method:

    String fileName = filePath;
    drillingArray[] drillerArray = new drillingArray[10];
    drillingRecord lineData;
    drillingArray recordHolder;
    BufferedReader br = new BufferedReader(new FileReader(fileName));
            
    int lineCount= 0;
    String line;
    line = br.readLine();
            
    while(line != null) {
        line=br.readLine();
        if(line != null) {
            String[] lineString = line.split(",");
            lineData = new drillingRecord(lineString);
            recordHolder = new drillingArray();
            recordHolder.add(lineData);
                
            if( lineCount==drillerArray.length){
                //possible issue in double drilling array although it does 
                //successfully double the size
                drillerArray = doubleDrillingArray(drillerArray);
            }
            drillerArray[lineCount]= new drillingArray(recordHolder);
            lineCount++;
        }
    }
        printData(drillerArray);
}

The double array size method bc that could be an issue although it appears to work

private static drillingArray[] doubleDrillingArray(drillingArray[] currentDrillingArray)  {
        int dbleSize = currentDrillingArray.length*2;
        
        if (dbleSize > 0 && dbleSize < Integer.MAX_VALUE) {
            drillingArray[] temp = new drillingArray[dbleSize];
            for (int i = 0; i < currentDrillingArray.length; i++) {
                temp[i] = currentDrillingArray[i];
                currentDrillingArray = temp;
            }
        }else{
            System.out.println("no space");
        }
        return currentDrillingArray;
    }

And the method to print

public static void printData(drillingArray[] array) {
        for(int i = 0;i<array.length;i++) {
            if(array[i] != null) {
                System.out.println(array[i].data[0]);
            }
            else {
                continue;
            }
        }
    }

I'm not sure where exactly data is getting overridden or copied inadvertently, and I would be extremely grateful if anyone with more experienced eyes could point me in the right direction. Let me know if you'd like any more information.

1 Answers

Some minor remarks:

  • I would really ditch those guidelines. Classes should be capitalized in java .

  • this dbleSize < Integer.MAX_VALUE could make sense if dbleSize was a long, but you actually used an int for it. You know what happens if you add +1 to Integer.MAX_VALUE ? It flips over, and it becomes Integer.MIN_VALUE. So, basically, when you multiply *2 the number could actually become smaller. I'm not saying this is likely to happen, cause Integer.MAX_VALUE really is a huge number. I'm just saying that the check doesn't make sense.

  • Having said that there are system functions to copy arrays. such as System.arraycopy(); They will also be faster.

  • Actually, what your code is, is exactly the behavior of an ArrayList. An ArrayList is an array that automatically resizes when necessary, each time doubling in capacity.

  • You're confusing yourself with the drillerArray vs drillingArray names. They are just too similar and already you're mixing them up in your code. According to your initial code a drillerArray contains drillingArray elements. However, you're using the doubleDrillingArray function to double the size of your drillerArray. You're really mixing both of them. It's not a bug yet.

Personally, what I wouldn't do

Having said all that, what is most confusing to me is the drillingArray class. The code isn't given however ...

        recordHolder = new drillingArray();
        ...
        drillerArray[lineCount]= new drillingArray(recordHolder);

So, a drillingArray (which is actually not an array but a class) can be constructed with a drillingArray parameter ? What does the constructor do with it ? Does it just copy it ?

I mean, can't this just be

drillerArray[lineCount]= recordHolder; // without creating a new one

The bug

And it's really inside that function that things go wrong.

currentDrillingArray = temp; should be moved one line down. i.e. it should be outside the for loop. You're assigning this with every iteration right now.

Because of the previous mistake the surrounding for-loop behaves really unpredictable. The source and destination arrays change, and the amount of iterations change while looping.

        drillingArray[] temp = new drillingArray[dbleSize];
        for (int i = 0; i < currentDrillingArray.length; i++) {
            temp[i] = currentDrillingArray[i];
            currentDrillingArray = temp;
        }

Let's simulate: for currentDrillingArray is {a, b, c}.

  1. the function creates a new array temp array {,,,,,}.
  2. The for loop is going to iterate while i < 3 (initially).
  3. In the first iteration it copies an element to temp: {a,,,,,} .
  4. And it also reassigns the currentDrillingArray to become {a,,,,,}. ! Which really is the bug
  5. Next, it will copy elements from the replaced currentDrillingArray ({a,,,,,}) to temp ({a,,,,,}).
Related