tags:

views:

73

answers:

6
public static void main(String[] args){
  Employee [] employeeList =
    {
    // build your list here
    };
}

how exactly do i build my array.

the array is just 2 strings that are defined in another class.

class Employee 
{
    protected String name;
    protected String jobTitle;          

    Employee(String n, String title)
    {
        name = n;                    
        jobTitle = title;
    }
}
A: 
Employee EmployeeList[] = new Employee[10]; // Creates an array of 10 Employee objects

edit, a more complete example:

class Employee
{
   protected String name;
   protected String jobTitle;

   Employee(String n, String title)
   { 
      name = n;
      jobTitle = title;
   }

   public static void main(String[] args){
      Employee employeeList[] =new Employee[10];

      Employee a = new Employee("a", "b");

      employeeList[0] = a;

      System.out.printf("%s %s\n", employeeList[0].name, employeeList[0].jobTitle);

   }

}
rascher
+1  A: 
Employee s[] = new Employee[]
{
    new Employee("a","b"), 
    new Employee("1","2")
};
Omry
+5  A: 
public static void main(String[] args){
    Employee[] employeeList = new Employee[] {
        new Employee("Name1", "Job1"),
        new Employee("Name2", "Job2"),
        new Employee("Name3", "Job3"),
        new Employee("Name4", "Job4")
    };
}
NawaMan
+2  A: 

You can just construct the objects

Employee [] employeeList =
  {
    new Employee("David", "CEO"),
    new Employee("Mark", "CTO")
  };

Or you can also do the following:

Employee[] employeeList = new Employee[2];
employeeList[0] = new Employee("David", "CEO");
employeeList[1] = new Employee("Mark", CTO");
notnoop
+2  A: 

errm, what??

I assume you simply want to build an array that holds employees? This is one way:

Employee [] employeeList = {new Employee("name", "title"), new Employee("name", "title")};
nkr1pt
+1 for the "errm, what?" question. I don't see this as answerable.
CPerkins
+1  A: 

If you don't know the size of the array ahead of time, you could use the ArrayList collection.

ArrayList<Employee> employeeList = new ArrayList<Employee>();

Then you can add as many Employees as you want as you go

employeeList.add(new Employee("a","b"));

The employees can be accessed by index similar to an array

tempEmployee = employeeList.get(0);

This class has a lot of other useful functions that would be otherwise difficult with just a straight array.

API: http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

FModa3