Results 1 to 4 of 4
  1. #1
    planesinspace is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default Problem with using an array in a constructor

    Hi, I am trying to create an ArrayList of 'Boys', and when I create these Boys, I want to give each boy a list of 3 numbers. I take in a text file which contains 3X3 integers. My constructor looks something like this:

    public class Boy
    {
    public int[] pref;
    public int value;

    public Boy(int value, int[] list)
    {

    this.pref=list;
    this.value=value;

    }

    Now, I attempt to make 3 Boys:

    currLine=new int[3];
    Scanner sc = new Scanner(new File("boyslist.txt"));
    for(int k=1;k<=3;k++)
    {

    for(int n=0;n<3;n++)
    {
    currLine[n]=sc.nextInt();

    }
    Boy boy = new Boy(k, currLine);


    When i add those boys to an ArrayList<Boy>, and try print each boy's preference list, it prints the same values for all three. So obviously my boy constructor isn't creating 3 separate ones.

    When I include this line:

    currLine=new int[3];

    so that it now looks like this:

    currLine=new int[3];

    for(int k=1;k<=3;k++)
    {
    currLine=new int[3];
    for(int n=0;n<3;n++)
    {
    currLine[n]=sc.nextInt();

    }
    Boy boy = new Boy(k, currLine);


    it works and each boy has a different set of preferences. But my question is why does this happen? I thought when I create a boy, I create a new array each time and copy over the values which were in the constructor, so I don't see why including that new initialisation each time the loop goes through makes any difference.

    ANy help much appreciated!

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    In the first case, you create both boys using the same array. There is only one such array in memory. Each boy has his own reference to that array but it's the same array object being pointed to in memory. When you modify the array through any one of the references, the changes are visible from all the other references because they are all pointing to the same array.
    The boys' references are different but they point to the same object in memory.

    You create a new array object with the second approach and so in this case the boys have different references that point to different objects.

  3. #3
    Nishchal is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default

    You tell it prints a same value ,but where is print statement.
    When i tried then this program runs correctly.I didn't found any problem in this code
    import java.util.*;
    import java.io.*;

    public class Boy
    {
    public int[] pref;
    public int value;

    public Boy(int value, int[] list)
    {
    this.pref=list;
    this.value=value;
    }

    public static void main(String [] a)
    {
    try
    {
    int[] currLine=new int[3];
    java.util.Scanner sc = new java.util.Scanner(new File("boyslist.txt"));
    for(int k=1;k<=3;k++)
    {
    for(int n=0;n<3;n++)
    {
    currLine[n]=sc.nextInt();
    }
    Boy boy= new Boy(k, currLine);
    }
    }
    catch(Exception e ){System.out.println("\n EXCEPTION "+e);}

    }
    }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Please use code tags when posting code.
    You did not understand the question posted or the answer I posted. The OP clearly wrote

    When i add those boys to an ArrayList<Boy>, and try print each boy's preference list, it prints the same values for all three. So obviously my boy constructor isn't creating 3 separate ones.
    You never added the boys to any ArrayList and therefore never observed the effects.

Similar Threads

  1. Problem with Constructor
    By ToastyBainey in forum New To Java
    Replies: 3
    Last Post: 03-09-2009, 02:36 AM
  2. Sending an array in a constructor?
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 09-14-2008, 09:59 PM
  3. Replies: 1
    Last Post: 07-20-2008, 04:42 PM
  4. Array Constructor
    By Javanoob828282 in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 10:25 PM
  5. Replies: 1
    Last Post: 12-14-2007, 07:25 AM

Posting Permissions

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