Results 1 to 6 of 6
- 12-12-2008, 01:29 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
[SOLVED] Arrays of Objects with Subscripts
Hi there, im trying to figure out how to how to display a list of the first names that are in an array of objects with subscripts, as a loop.
shown below
Java Code:Employee names[] = new Employee[5]; names[0].setFirstName("John"); names[0].setLastName("doe"); names[0].setID("1234"); names[1].setFirstName("Mary"); names[1].setLastName("berry"); names[1].setID("2345"); names[2].setFirstName("Chris"); names[2].setLastName("bliss"); names[2].setID("3456"); names[3].setFirstName("Angela"); names[3].setLastName("Jolie"); names[3].setID("4567"); names[4].setFirstName("Marky"); names[4].setLastName("mark"); names[4].setID("5678"); //This is what ive tried but it does not work for (int i = 0; i < names.length; i++) System.out.println(names[i].getFirstName());
Edit: this accesses another file Employee, with methods already.
-
Does this Employee class have a toString() method? If so, you might be able to display it quite simply:
Java Code:for (int i = 0; i < names.length; i++) { System.out.println(names[i]); }
- 12-12-2008, 01:40 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
-
then your code has a bug and the names object is null here. you have to fix this.
In your code above, I don't see you creating any "new" Employee objects, just a new array. Please know that when you create an array of objects (as opposed to an array of primitive such as ints), you have to create a new object for each item in the array.
- 12-12-2008, 02:15 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
ahhh I see what you did there ;P
got it
graciasJava Code:Employee[] names = new Employee[5]; names[0] = new Employee("John", "doe", "1234"); names[1] = new Employee("Mary","berry", "2345"); names[2] = new Employee("Chris", "bliss", "3456"); names[3] = new Employee("Angela", "Jolie", "4567"); names[4] = new Employee("Marky", "mark", "5678");Last edited by Sidmyre; 12-12-2008 at 02:18 AM.
-
Similar Threads
-
Two Objects
By losintikfos in forum New To JavaReplies: 3Last Post: 11-14-2008, 07:04 PM -
1 to 1 Objects
By this.that in forum New To JavaReplies: 4Last Post: 08-07-2008, 10:09 PM -
how many objects ?
By kevinsong in forum Advanced JavaReplies: 16Last Post: 07-16-2008, 05:59 PM -
Getting objects from a list
By markyoung1984 in forum New To JavaReplies: 4Last Post: 03-13-2008, 10:45 PM -
Help with Objects!
By Shorinhio in forum New To JavaReplies: 1Last Post: 07-10-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks