0

In our current chapter we are using arrays which I'm having some trouble creating a listing to be called upon from another class.

Goal: Display the parallel arrays from another class, this can be singular or in a group.

Question: Best or efficient way to call on a multi-parallel array with different data types?

Error: Starts with an illegal statement, as previously instructed here is the whole code, please ignore the loop display I was just testing to make sure the arrays were setup correctly.

Thanks Everyone, Yet again any assistance is much appreciated

import java.util.ArrayList;

public class Employee {

    public static void main(String[] args) {

// create an array with employee number, first name, last name, wage, and Skill
        int[] empID = {1001, 1002, 1003};
        String[] firstName = {"Barry", "Bruce", "Selina"};
        String[] lastName = {"Allen", "Wayne", "Kyle"};
        double[] wage = {10.45, 22.50, 18.20};
        String[] skill = {"Delivery Specialist", "Crime Prevention", "Feline Therapist"};
        /*
for ( int i = 0; i < empID.length; i++ )
{
System.out.print( "Employee ID: " + empID[i] + "\n");
System.out.print( "First Name: " + firstName[i] + "\n");
System.out.print( "Last Name: " + lastName[i] + "\n");
System.out.print( "Hourly Wage: $" + wage[i] + "\n");
System.out.print( "Skill: " +skill[i] );
System.out.println("\n");
}
         */
        //create an object to be called upon from another class
public ArrayList<int, String, String, double, String> getEmployee() {
        ArrayList<int, String, String, double, String> employeeList = new ArrayList<int, String, String, double, String>();
        employeeList.add(empID);
        employeeList.add(firstName);
        employeeList.add(lastName);
        employeeList.add(wage);
        employeeList.add(skill);

        return employeeList;
    }

}
} //end of class
4
  • 1
    ArrayLists can only have one type parameter. I would recommend making the employee class separate and providing its appropriate properties. Commented Apr 22, 2017 at 13:11
  • oh so make 3 display methods, String, Int, Double... that makes sense Commented Apr 22, 2017 at 13:12
  • @Ousmane The instructions say build a class with arrays: employee ID, First, Last, Wage, Skill. Build another class and display the information. Commented Apr 22, 2017 at 13:14
  • 1
    see the answer below. Commented Apr 22, 2017 at 13:14

2 Answers 2

1

First, you can't declare an ArrayList like this:

ArrayList<int, String, String, double, String>

If you want that, you can create your own object, create a class which can take these values, then you can create an ArrayList of this Object for example:

class MyClass{
   int att1;
   String att2;
   String att3;
   double att4;
   String att5;

    public MyClass(int att1, String att2, String att3, double att4, String att5) {
        this.att1 = att1;
        this.att2 = att2;
        this.att3 = att3;
        this.att4 = att4;
        this.att5 = att5;
    }
}

Then you can create an ArrayList like this :

List<MyClass> list = new ArrayList<>();
Sign up to request clarification or add additional context in comments.

1 Comment

@ Ousmane & @ YCF Thank you both very much, attempting the alternations now!
0

Going back to the basics of Java, it is an object oriented programming language so you should always aim to abstract "things" into an object if possible. You should encapsulate all of the common properties about an 'employee' into a class with all of the data as fields.

As the answer above shows, creating an ArrayList<MyClass> is the proper way of initialising arraylists as they can only take ONE type of data. You may have seen other classes take multiple types such as HashMap<Type1, Type2> but these are for a specified reason. Make sure to check the API doc first!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.