|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-26-2008, 08:42 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 3
|
|
|
Can I store multiple objects in an array
I am working on an array and want to store entries that will have multiple objects - for example a student, a student with a car, etc.
I know the syntax to store one object, but not multiples.
|
|

05-27-2008, 05:03 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-27-2008, 05:07 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 3
|
|
|
Multiple object in an array
I am new this this forum, so wasn't quite sure what or how to ask.
I managed to solve this by storing objects within objects prior to storing within an array. For example, I put the car object (which contains brand, cost & miles) into the student object prior to putting into the array.
|
|

05-27-2008, 06:33 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
Can you show your code, what you have done up to now.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-27-2008, 12:07 PM
|
 |
Member
|
|
Join Date: May 2008
Location: LV
Posts: 36
|
|
|
Object[] arr = new Object[10];
arr[0]="Ordinary String";
arr[1]=new Integer(77);
System.out.println((String)arr[0]);
if (arr[1] instanceof Integer) System.out.println(((Integer)arr[1]).toString());
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-28-2008, 04:57 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 3
|
|
|
Here is my solution
I pass information to this method which can include just the student info or the student info and either a credit card object or a car object, which then gets stored in the array.
//An array for holding students and their information
//about credit cards and cars.
protected Student[] studentCollection;
//for holding the total number of items in Student array
protected int manyItems = 0;
/**
* Method to create classroom based on size from tester
*/
public ClassRoom (int size)
{
StudentCollection = new Student[size];
}
/**
* Method to populate array with only Student information
*/
public void addStudent (Student newStudent, int index)
{
studentCollection[index] = newStudent;
manyItems++;
}
/**
* Method to populate array with Student & CreditCard information
*/
public void addStudent (Student newStudent, CreditCard newCreditCard,
int index)
{
studentCollection[index] = newStudent;
manyItems++;
}
/**
* Method to populate array with Student & Car information
*/
public void addStudent (Student newStudent, Car newCar, int index)
{
studentCollection[index] = newStudent;
manyItems++;
}
|
|

05-28-2008, 11:58 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 21
|
|
|
any array or any thing other then primitive types are objects in java so u can store any thing in an object array .
__________________
get new coding problems at To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-29-2008, 05:51 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
Including strings too. It's not a primitive data type. Most of the people confused on it.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-29-2008, 05:55 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
you could store primitive too if you do new Integer(42)
__________________
My IP address is 127.0.0.1
|
|

05-29-2008, 05:57 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
Yes thats true.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|