Current location: Hot Scripts Forums » Programming Languages » Everything Java » Quick question dealing with object arrays


Quick question dealing with object arrays

Reply
  #1 (permalink)  
Old 01-04-05, 10:28 PM
dmiranda dmiranda is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Quick question dealing with object arrays

Hi all,

Here's what Im trying to do:
I need to create an array of objects. I need to be able to reference them in order by using a for loop index or just sequentially. I have a class called 'employee', and a class for processing called 'collection'. I need to be able to create multiple instances of employee and shove them into an array. I also want to be able to read that array and pull properties of any instance in the array.

Here's what I've got so far:

public class Employee {

public String employeeName;
public String employeeSalary;
public String employeeDate;

}

import java.util.*;

public class Collection{

public static void main(String args[]) {
ArrayList aList = new ArrayList();
Object objArray[] = new Object[5];
for (int i = 0; i < 4; i++) {

Employee emp = new Employee();
emp.employeeName = "Fred";
emp.employeeSalary = "500000";
emp.employeeDate = "Jan 1";
//System.out.println(emp.employeeSalary);
objArray[i] = emp;
// aList.add(emp);
}

for (int i = 0; i < 4; i++) {
System.out.println(objArray[i]);
}
}
}

I've been trying multiple methods to create an object array to no avail. I believe that I should use an array list with an iterator, but havent found any substantial code online to help me do that.

Just a simple example of populating and reading properties from an object array would be awesome. Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-05-05, 08:29 AM
squeebie's Avatar
squeebie squeebie is offline
Newbie Coder
 
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
What you have doesn't work?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 01-09-05, 05:15 AM
Rob_Darkins Rob_Darkins is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Shouldn't you declare 'i < 5' in the FOR loops? Rather than 'i < 4' ? As, you've declared an array of five elements to process, yet, you're stopping in the FOR loops at the fourth array (element 3).
So, either change the 'i < 4' to 'i < 5', or add an equal sign, so that it shows:
'i <= 4' instead of 'i < 4'

HTH
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Question... nuzzle PHP 7 12-02-04 01:15 PM
Quick question dmiranda Windows .NET Programming 2 10-19-04 11:50 PM
A quick Question jbsniff PHP 8 10-12-04 10:44 AM
Quick Question for you php guru's Tokahashi PHP 3 04-09-04 01:00 PM
Object Oriented Programming Stefan PHP 29 12-30-03 12:22 PM


All times are GMT -5. The time now is 01:41 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.