passing arrays to classes?

Reply

Join Date: Oct 2004
Posts: 44
Reputation: ultimate_fusion is an unknown quantity at this point 
Solved Threads: 0
ultimate_fusion ultimate_fusion is offline Offline
Light Poster

passing arrays to classes?

 
0
  #1
Feb 15th, 2005
//main method
int number = 1;
int num = 3;
details[] Darray = new details[number][num];

String name = JOptionPane.showInputDialog(null,
"Enter the name",
"Enter details",
JOptionPane.QUESTION_MESSAGE);
String DOB = JOptionPane.showInputDialog(null,
"Enter the DOB",
"Enter details",
JOptionPane.QUESTION_MESSAGE);

class details
// i have default construtors aswell
String getName(){
return name;
}
heres the problem - i was told that you are only ment to input data in the main method. I need a 2d array to store many people info.
I tried Darray[0] = name; but this came up
C:\Documents and Settings\java:34: incompatible types
found : java.lang.String
required: details

since classes are ment to store information (possibley privately) I was wondering the easiest way to store it in a array and pass it. I also want to use the array in the main method after the have finished storing it.
any ideas?

thanks for looking
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: passing arrays to classes?

 
0
  #2
Feb 16th, 2005
Darray is a multidimential array of class details so you neede to use Darray[0][0] something like that in order to access details object in Darray.
Darray[0][0] = name won't work because java compiler can't convert string to details.
i am not sure but you might need to initilize every object in the array before you are going to use them. May be java compiler does this for you by calling default constructer for each element in array...
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: passing arrays to classes?

 
0
  #3
Feb 17th, 2005
Java will initialise all elements of an array to null when you create the array itself.

Your Details (ALWAYS, ALWAYS, ALWAYS capitalise classnames) class should have a public setName(String name) method.
You should indeed also initialise the array elements yourself.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: passing arrays to classes?

 
0
  #4
Feb 17th, 2005
primitive type like int, char it is not assigned to null but zero or space, when creating arrays of these types. so,
int[] x = new int[5]; //all elements are equals to zero....

but like jwenting said when you make an array of objects they are all set to null, you need to construct each of them.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC