Results 1 to 4 of 4
- 08-26-2009, 03:30 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
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!
- 08-26-2009, 03:38 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 08-28-2009, 09:10 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
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);}
}
}
- 08-28-2009, 09:17 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Please use code tags when posting code.
You did not understand the question posted or the answer I posted. The OP clearly wrote
You never added the boys to any ArrayList and therefore never observed the effects.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.
Similar Threads
-
Problem with Constructor
By ToastyBainey in forum New To JavaReplies: 3Last Post: 03-09-2009, 02:36 AM -
Sending an array in a constructor?
By dch414 in forum New To JavaReplies: 2Last Post: 09-14-2008, 09:59 PM -
[SOLVED] Unable to access array defiened in constructor in other methods.
By Shyam Singh in forum New To JavaReplies: 1Last Post: 07-20-2008, 04:42 PM -
Array Constructor
By Javanoob828282 in forum New To JavaReplies: 1Last Post: 04-30-2008, 10:25 PM -
problems with asigning elements of an array to a constructor
By rednessc in forum New To JavaReplies: 1Last Post: 12-14-2007, 07:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks