Reputation: 547
OK, I have a bit of a conundrum. I'll say straight out that I'm working on a homework assignment and I've come to a stumbling point. I'm sure that I'm missing something obvious, but after hours of searching the internet and text books to try and find an answer to this, I'm butting up against a wall and I'm hoping someone can point me in the right direction.
I have created a class called "employee" that defines an employee object, it has getter and setter methods for the employee's name and sales totals. It looks as follows:
public class employee {
private String employeeName;
private double yearSales;
public employee(String employeeName, double yearSales)
{
this.employeeName = employeeName;
this.yearSales = yearSales;
}
public void setName(String employeeName)
{
this.employeeName=employeeName;
}
public void setSales(double yearSales)
{
this.yearSales=yearSales;
}
public String getEmployee()
{
return employeeName;
}
public double getYearsSales()
{
return yearSales;
}
}
I then have a method that is intended to instantiate an ArrayList that contains employee objects. I'm able to get as far as creating the ArrayList and adding information to it as shown below:
public ArrayList employeeArray(String name, double sales)
{
//Instantiate a new ArrayList object
ArrayList employeeList = new ArrayList();
//Initialize the values in the ArrayList
employeeList.add(new employee(name, sales));
return employeeList;
}
Where I am running into trouble is with attempting to print out the name value from the ArrayList
, shown below:
System.out.println(employeeList.get(0).getEmployee());
I'm only adding one element so the index value should be correct, and I worked with ArrayLists in another Java course not too long ago and was able to do something similar to this in my code for those assignments. If I need to clarify anything more about this I'll be happy to. Of course, any assistance on this is greatly appreciated.
Upvotes: 1
Views: 2585
Reputation: 6783
Youre trying to instantiate a new ArrayList
with each call to employeeArray()
method. Try maintaining a common ArrayList
and add elements to it using this method.
Also +1 with using generics
And if you're new to Java, then do read this link as well: "Java Programming Style Guide (Naming Conventions)"
Assuming you've a class called EmployeeList
where you have defined this method employeeArray()
, you can update it to maintain new names in the list as follows (note that this is one sample solution, you are obviously welcome to tailor it to your need):
public class EmployeeList{
private ArrayList<Employee> employeeList;
public EmployeeList(){
//Initializing the employee arraylist
employeeList = new ArrayList<Employee>();
}
public ArrayList<Employee> employeeArray(String name, double sales){
//Initialize the values in the ArrayList
employeeList.add(new Employee(name, sales));
return employeeList;
}
}
Also note the use of generics and the naming conventions in the above code. This might be helpful for you.
Upvotes: 0
Reputation: 91329
You should be using Generics if you have Java SE >= 5, so instead of ArrayList
, use ArrayList<employee>
. Otherwise, you'd need to cast its type from Object
to Employee
:
System.out.println(((employee)employeeList.get(0)).getEmployee());
Also, class and interface names in Java should start with an uppercase letter.
Upvotes: 4
Reputation: 2080
public ArrayList<employee> employeeArray(String name, double sales)
{
//Instantiate a new ArrayList object
ArrayList<employee> employeeList = new ArrayList<employee>();
//Initialize the values in the ArrayList
employeeList.add(new employee(name, sales));
return employeeList;
}
Upvotes: 3