Results 1 to 10 of 10
- 05-26-2008, 06:42 PM #1
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
- 05-27-2008, 03:03 AM #2
- 05-27-2008, 03:07 AM #3
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
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, 04:33 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you show your code, what you have done up to now.
- 05-27-2008, 10:07 AM #5
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());Mattery - free online strategy game
- 05-28-2008, 02:57 AM #6
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
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, 09:58 PM #7
Member
- Join Date
- May 2008
- Posts
- 21
- Rep Power
- 0
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 CODERWORDS
- 05-29-2008, 03:51 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Including strings too. It's not a primitive data type. Most of the people confused on it.
- 05-29-2008, 03:55 AM #9
you could store primitive too if you do new Integer(42)
My IP address is 127.0.0.1
- 05-29-2008, 03:57 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
How do you read from a file, and then store the info in an array?
By szimme101 in forum New To JavaReplies: 5Last Post: 07-30-2008, 09:30 AM -
Can I use vectors to store multiple types of objects
By Nathand in forum Advanced JavaReplies: 6Last Post: 04-28-2008, 07:55 AM -
How would you get information from a file and then store it in an array?
By szimme101 in forum Advanced JavaReplies: 3Last Post: 04-07-2008, 06:02 PM -
questions about using array to store profile
By hien_NU in forum New To JavaReplies: 6Last Post: 01-08-2008, 05:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks