OVERTONE
OVERTONE

Reputation: 12187

Passing arrays into testclass constructors

I need to pass a 1d array that isn't defined in a method.

I need to create a testclass then make the arrays myself.

I'm just not sure about the syntax.

Example, here's 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? Do do I put arrays(employees) into it?

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

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

Upvotes: 1

Views: 3541

Answers (2)

kashif
kashif

Reputation: 1

Empolyee [] employeeList = new Employee[2];


for(int i=0;i<2;i++){
  Scanner input = new Scanner(System.in);

  employeeList[i] = input.next();
}

Upvotes: 0

Daniel Bingham
Daniel Bingham

Reputation: 12914

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");

Upvotes: 1

Related Questions