views:

207

answers:

2

lo.

first time posting on stack.

im trying too just do some past exam questions but its proving difficult so sorry if this sounds noobish.

i need too pass a 1d array that isnt defined in a method. i need too create a testclass then make the arrays myself.

im just not sure about the syntax.

example heres my company class

public class Company
{
String name;
String address;
Employee employeeList[] = new Employee[3];

public Company (String name, String address ,
                     Employee employeeList, String jobTitle )
{
 this.name = name;
 this.address = address;
}
public void printDetails()
 {
  for(int i = 0; i>employeeList.length;i++)
  {
   System.out.println(" The companys name is " + name);
   System.out.println(" The Companys Address is "+ address);
   System.out.println("The List of employees are " + employeeList[i].name);
   System.out.println("The Titles of These Employees are " + employeeList[i].jobTitle);
  }
 }
}

but my testclass is where the problem lies.

where do i go from here with how too put arrays(employees) into it,

public class TestCompany
{
public static void main(String[] args)

{                                                                        employees?
Company hungryBear = new Company("hungryBear ", "Those weird apartments ",//////   );
}
}

help me o guru's of computer wisdom.

A: 
public Company (String name, String address ,
                     Employee employeeList, String jobTitle )

Should be:

public Company (String name, String address ,
                     Employee []employeeList, String jobTitle )

Right now, you're not passing an array to your method, your passing an instance. You need to tell Java that you're passing an array.

Editted with new knowledge of the employee class...

Also, you will need to build the array in your main function before you pass it. Something like this:

public static void main(String[] args){                                                                        
    Employee [] employeeList = {
        new Employee("Samuel T. Anders", "Player, Caprica Buccaneers"),
        new Employee("William Adama", "Commander, Battlestar Galactica")
    };

    Company hungryBear = new Company("hungryBear ", "Those weird apartments ", employeeList);
}

Not really sure this answers your question, but maybe this will help you with the syntax of array passing a little.

Another edit, another way to initialize an array:

Empolyee [] employeeList = new Employee[2];
employeeList[0] = new Employee("Samuel T. Anders", "Player, Caprica Buccaneers");
employeeList[1] = new Employee("William Adama", "Commander, Battlestar Galactica");
Daniel Bingham
thanks. just one more thing
OVERTONE
Editted the answer. You can also initialize the array with the number of values you have and then set each cell individually.
Daniel Bingham
A: 

thanks so much al.

but theres just one mroe thing

Employee [] employeeList = {
// build your list here
};

how do i build the list?

is it like?

Employee [] employeeList = { "john","manager"
};

also i never gave my employee class.

this is it. theres just 2 string i need too pass in.

class Employee { protected String name; protected String jobTitle;

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

}

OVERTONE
Employee[] empList = { new Employee("john", "manager"), new Employee("mary", "receptionist") }; btw you can always make a second question if you have more questions
wds