Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.
import java.util.Scanner;
public class ResizeArrays {
public static void main(String[] args) {
Scanner inputScan = new Scanner(System.in);
System.out.println("Please enter the number of emloyees:");
int numberOfEmployees = inputScan.nextInt();
System.out.println("Please enter the number of days worked each week:");
int numberOfDaysWorked = inputScan.nextInt();
// Arrays will be changed to dynamic arrays according to input values:
Integer[][] hoursWorkedArray = new Integer[1][1];
Integer[] totalHoursArray = new Integer[1];
String[] employeeNamesArray = new String[1];
// change arrays sizes here:
employeeNamesArray = resizeStringArray(employeeNamesArray, numberOfEmployees); // WORKING
totalHoursArray = resizeIntArray(totalHoursArray, numberOfEmployees); //BAD
hoursWorkedArray = resizeInt2DArray(hoursWorkedArray, numberOfEmployees, numberOfDaysWorked ); // BAD
// Check array sizes:
System.out.println("employeeNamesArray size: " + employeeNamesArray.length);
System.out.println("totalHoursArray size: " + totalHoursArray.length);
System.out.println("hoursWorkedArray sizes: Rows= " + hoursWorkedArray.length + " Columns= " + hoursWorkedArray[0].length);
// arrayName.length the length attribute returns the number of rows
// arrayName[i].length will return the number of columns in row i
}
// resize a single-dimensional String [] array:
public static String[] resizeStringArray(String[] arrayToBeResized, int newSize) {
String[] temp = new String[newSize];
System.arraycopy(arrayToBeResized, 0, temp, 0, arrayToBeResized.length);
for(int j = arrayToBeResized.length; j < newSize; j++)
temp[j] = "";
return temp;
}
// resize a single-dimensional int[] array:
public static int[] resizeIntArray(int[] arrayToBeResized, int newSize) {
int[] temp = new int[newSize];
System.arraycopy(arrayToBeResized, 0, temp, 0, arrayToBeResized.length);
for(int j = arrayToBeResized.length; j < newSize; j++)
temp[j] = 0;
return temp;
}
// resize 2-dimensional int[][] array:
private static int[][] resizeInt2DArray(int[][] arrayToBeResized, int newIndex1Size, int newIndex2Size ){
int[][] temp = new int[newIndex1Size][newIndex2Size];
// ????????????????????????????????????????????
return temp;
}
}
With bad code commented:
Please enter the number of emloyees:
6
Please enter the number of days worked each week:
7
employeeNamesArray size: 6
totalHoursArray size: 1
hoursWorkedArray sizes: Rows= 1 Columns= 1
Process completed.
int[] totalHoursArray = new int[numberOfEmployees];
int[][] hoursWorkedArray = new int[numberOfEmployees][numberOfDyas];
String [] empoloyeeNames = new String[numberofEmployees];
and youprobnably need int instead of Integer// resize 2-dimensional int[][] array:
private static int[][] resizeInt2DArray(int[][] arrayToBeResized, int newIndex1Size, int newIndex2Size) {
int[][] temp = new int[newIndex1Size][newIndex2Size];
for (int i = 0; i < arrayToBeResized.length; i++) {
final int[] arrayI = arrayToBeResized[i];
System.arraycopy(arrayI, 0, temp[i], 0, arrayI.length);
}
return temp;
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.