0

I have an Array of Objects (a name, and an array of numbers) that is not storing new Objects in the Array correctly. Each time I attempt to add a new object with a for loop it is overwriting the previous objects. Please forgive me as I am a novice, and have been stuck for hours on this problem.

        /////INITIALIZE THE DATA/////////
    //  Read the Data and Return an Array of Objects from the text File
    // Read in the info and calculate number of total lines.
    Scanner scanFile1 = new Scanner(new FileReader("names2.txt"));
    while (scanFile1.hasNextLine()) 
    {
        scanFile1.nextLine();
        lines++;
    }
    scanFile1.close();

    // Create array of objects  
    Scanner scanFile2 = new Scanner(new FileReader("names2.txt"));
    nameArray = new Name[lines];

    String tempName;
    int[] tempArray = new int[DECADES];

    for (int n = 0; n < lines; n++)
    {   
        tempName = scanFile2.next();
        for (int i = 0; i < DECADES; i++)
        {
            tempArray[i] = scanFile2.nextInt();
        }

        nameArray[n] = new Name(tempName, tempArray);
        System.out.println(n);
        System.out.println(tempName);
        System.out.println(Arrays.toString(tempArray));
        System.out.println(Arrays.toString(nameArray[0].popularityRanks));      


        scanFile2.nextLine();       
    } 

    scanFile2.close();

When I step through the code, printing out the changes as they happen I see that the items in location nameArray[0] keep getting loaded with the latest set of data being read from a text file. Here is the text contents for reference.

    Bob 83 140 228 286 426 612 486 577 836 0 0
    Sam 0 0 0 0 0 0 0 0 0 380 215
    Jim 1000 999 888 777 666 555 444 333 222 111 100

And here is a printout of the changes as they happen (prints index of Array, new name, new numbers for second part of object, and numbers in location 0 of the Array)

    0
    Bob
    [83, 140, 228, 286, 426, 612, 486, 577, 836, 0, 0]
    [83, 140, 228, 286, 426, 612, 486, 577, 836, 0, 0]
    1
    Sam
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 380, 215]
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 380, 215]
    2
    Jim
    [1000, 999, 888, 777, 666, 555, 444, 333, 222, 111, 100]
    [1000, 999, 888, 777, 666, 555, 444, 333, 222, 111, 100]

The Name class is as follows:

public class Name 
{
public static final int DECADES = 11;
public static final int DECADE1900 = 0;
public static final int DECADE1910 = 1;
public static final int DECADE1920 = 2;
public static final int DECADE1930 = 3;
public static final int DECADE1940 = 4;
public static final int DECADE1950 = 5;
public static final int DECADE1960 = 6;
public static final int DECADE1970 = 7;
public static final int DECADE1980 = 8;
public static final int DECADE1990 = 9;
public static final int DECADE2000 = 10;

public String name = "err";
public int[] popularityRanks;


public Name (String name, int[] popularityRanks) 
{
    this.name = name;
    this.popularityRanks = popularityRanks;
}   

    //...more methods to assess and work with the class...
}

Thanks ahead of time, this site has been so useful, I've never needed to post here until now, on my last assignment.

1
  • the class named Name is misnamed! google 'naming conventions software' Commented Apr 29, 2013 at 11:19

2 Answers 2

1

An array is an object. If you resuse the same array then you will over write it. Just passing it in constructor does not make it new. The Object name it seems is just storing reference to the same :

  tempArray = new int[DECADES];

What you need to do Either :

  • in the Name object make a copy of the array passed, using System.arrayCopy or API in java.util.Arrays or use a new List add all

OR

  • ADD the line

    tempArray = new int[DECADES];
    

After the line

nameArray[n] = new Name(tempName, tempArray);

Would help to see the code of Name class.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't want to overwhelm if no one was going to be available, but man you folks are like ninjas. I'll edit the original post to include the Name class. I'll also investigate your recomendations...
I followed your instructions on the second line and it worked perfectly. I will clean up the thread to remove the Google+ request info and mark the issue as solved. Great Work!
1

The problem is that when you do this outside of the loop:

int[] tempArray = new int[DECADES];

Only one array is made in memory. Arrays are considered objects in Java, and when you assign variables to an array, it does not copy the array.

So when you do this:

nameArray[n] = new Name(tempName, tempArray);

You are passing the new Name a new reference to the same tempArray. So when you modify it, it's no surprise that 'all the other arrays' are modified - they are really the same array.

To fix this, make the array IN the for loop, not out of it.

1 Comment

Thanks for the quick response, I think that is what tgkprog reccomended and it worked like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.