0

This is a code taken from Cracking the coding interview.I didnt understand line number 4 and 5. From what I can see ,it is arraylist of arraylist but bit confused about how it is defined. Please explain me whats actually happening in these lines.

1 public class CallHandler {

2 static final int LEVELS = 3; // we have 3 levels of employees

3 static final int NUM_FRESHERS = 5; // we have 5 freshers

4 ArrayList<Employee>[] employeeLevels = new ArrayList[LEVELS];

5 // queues for each call’s rank

6 Queue<Call>[] callQueues = new LinkedList[LEVELS];
7
8 public CallHandler() { ... }
9
10 Employee getCallHandler(Call call) {

11 for (int level = call.rank; level < LEVELS - 1; level++) {

12 ArrayList<Employee> employeeLevel = employeeLevels[level];

13 for (Employee emp : employeeLevel) {

14 if (emp.free) {

15 return emp;

16 }

17 }

18 }

19 return null;

20 }

5 Answers 5

2

This is creating an Array (not ArrayList) of ArrayLists. As the code is commented, there are 3 levels of employees. They're storing each level in one of the 3 ArrayLists in the employeeLevels array.

Alternatively, they could have stored each employee level in its own variable - but this certainly isn't as scalable or flexible. It also would make things like looping over each level (as they are, starting at line #11-12) much more difficult.

Sign up to request clarification or add additional context in comments.

Comments

2
ArrayList<Employee>[] employeeLevels = new ArrayList[LEVELS];

From what I can see ,it is arraylist of arraylist but bit confused about how it is defined.

It is an array of ArrayLists.

It has length LEVELS (3 in your code), all elements are initially null. To use these slots, you have to put in ArrayLists (does not happen in the code you showed).

To get to an individual of these lists, do

ArrayList<Employee> employeeLevel = employeeLevels[level];

but initialize the entry first, or you will just get null (not an empty List).

 ArrayList<Employee>

The stuff in angle brackets is a generic type annotation. It means that the ArrayList can only store instances of Employee (or subclasses thereof).

So to sum up, you have an array with three elements, each of which is a List that can store Employee instances, or it can be null. The "three" lists do not need to be distinct (you can put the same list in all three slots).

Comments

0

ArrayList[] employeeLevels = new ArrayList[LEVELS];

ArrayList[] <--- this is the type definition

new ArrayList[LEVELS]; <--- this is the instantiation.

Comments

0

From a Design view point

If you create a ArrayList, you basically add a lot of flexibility to code. So that you dont need to manage arraylist sizes. If you create ArrayList[], you are putting a limit on one dimension of a 2D array. That way you know for sure that one dimension is fixed. So kinda saving some memory and allowing each ArrayList in your array the flexibility to grow.

Comments

0

Another point should be noticed about the definition. It is a good practice for most developers to code against interfaces, not implementations. So the definition should look like List<Employee>[] employeeLevels = new ArrayList[LEVELS];

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.