0

I made 2 1-D arrays, and want to sort the values of a 2-D array so they can divide amongst the 1-D arrays.

For context, I'm making a program that utilizes user input to create a 2-D array organizing salaried and hourly employees.

The user inputs the number of employees they want to store in the employeeDatabase. Then they input the ID numbers (employeeList) and finally whether each employee is salaried or hourly (1 or 2) This data is then stored into a 2-D array.

As an example, here's a sample employeeDatabase.

EmployeeID Salaried (1) or Hourly (2)
123456 1
654321 2

I want it to output: Employee 123456 is a salaried employee. Employee 654321 is an hourly employee.

I want to separate the values in the employeeDatabase into arrays salariedEmployees and hourlyEmployees but I keep getting "ArrayIndexOutOfBoundsException: 0" arrays. The snippet of code that causes problems is below. Thank you!

      int salariedEmployeesSize = 0;
      int hourlyEmployeesSize = 0;
      
      int[] salariedEmployees = new int[salariedEmployeesSize];
      int[] hourlyEmployees = new int[hourlyEmployeesSize];
      
      for (int i = 0; i < employeeList.length - 1; i++) {
         if (employeeDatabase[i][1] == 1) {
            salariedEmployeesSize += 1;
            salariedEmployees[i] = employeeDatabase[i][0];
            
         } else if (employeeDatabase[i][1] == 2) {
            hourlyEmployeesSize += 1;
            hourlyEmployees[i] = employeeDatabase[i][0];
           
         }
      }


      for (int i = 0; i < salariedEmployees.length - 1; i++) {
         System.out.println("Employee " + salariedEmployees[i] + " is a salaried employee.");
      }     
      
      for (int i = 0; i < hourlyEmployees.length - 1; i++) {
         System.out.println("Employee " + hourlyEmployees[i] + " is an hourly employee.");
      }

6
  • 1
    Do you assume your array increases its size because your change the value of hourlyEmployeesSize? It doesn't, hourlyEmployees will still be an array of size 0. Commented Jul 22, 2021 at 1:58
  • 2
    If you want to dynamically add elements then you should be using an ArrayList rather than an Array, or you should define the size of the array correctly before adding elements. Commented Jul 22, 2021 at 2:06
  • 2
    More to the point, you're always creating the output arrays with size zero. The choice of arrays to solve this problem is a bad one. Consider looking at a Map in place of the 2d table and List for the 1d ones. You'll be amazed at how easy what you're trying to accomplish becomes. Commented Jul 22, 2021 at 2:07
  • I want to utilize 2-D and 1-D arrays for this problem if it's possible. I want hourlyEmployeesSize and salariedEmployeesSize to increase: is anything stopping that? Commented Jul 22, 2021 at 2:13
  • @jcpark, the solution will be much cleaner and in acordance to java's best practices to use the java api packages, such as java.util, is there a reason for you to not use this package, and which java version are you using? Commented Jul 22, 2021 at 2:32

1 Answer 1

1

You have to resize the array each time you add an element. in java, this is done by:

  1. Creating a new array with the new size.
  2. Moving the elements to the new array.
  3. Overwrite the variable value that reference the old array with the new array.

The old array will not be referenced anymore and will be destroyed by the jvm's garbage collector.

Note: Your Question will probably be marked as duplicate, I encourage you to search for the answers :).

References you can search on:

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

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.