如何防止覆盖并添加到现有数组(java)

wqsoz72f  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(481)

我的项目主要按照预期工作,除了我的方法将覆盖初始条目而不是将新条目添加到数组中这一事实。
例如,如果我通过选项1输入2个条目,然后尝试通过选项2添加另一个(单个)条目,索引0将被新条目覆盖。
我已尝试将类设置为“final”以防止覆盖,但不确定在使数组具有附加功能方面会出现什么错误:

  1. import java.util.Scanner;
  2. public final class ProjectTest {
  3. //Create Method Arrays
  4. final static int [] EmployeeID = new int[99];
  5. final static double [] EmployeeSalary = new double [99];
  6. final static String [] EmployeeFirst = new String [99];
  7. final static String [] EmployeeLast = new String [99];
  8. private static Scanner scan;
  9. //Method Add MultiEmployee
  10. private final static void MultiEmployee () {
  11. scan = new Scanner(System.in);
  12. System.out.println("\nHow many employees would you like to enter?: ");
  13. int EmployeeCount = scan.nextInt();
  14. for (int i = 0; i < EmployeeCount; i++) {
  15. System.out.println("\nEmployee " + (i+1)+":");
  16. System.out.println("\nEnter employee name (First Last):");
  17. EmployeeFirst[i] = scan.next();
  18. EmployeeLast[i] = scan.next();
  19. System.out.println("\nEnter Employee ID#:");
  20. EmployeeID[i] = scan.nextInt();
  21. System.out.println("\nEnter Employee Salary:");
  22. EmployeeSalary[i]=scan.nextDouble();
  23. }
  24. }
  25. //Method Add SingleEmployee
  26. private final static void SingleEmployee () {
  27. for (int i = 0; i < 1 ; i++) {
  28. scan = new Scanner(System.in);
  29. System.out.println("\nEmployee " + (i+1)+":");
  30. System.out.println("\nEnter employee name (First Last):");
  31. EmployeeFirst[i] = scan.next();
  32. EmployeeLast[i] = scan.next();
  33. System.out.println("\nEnter Employee ID#:");
  34. EmployeeID[i] = scan.nextInt();
  35. System.out.println("\nEnter Employee Salary:");
  36. EmployeeSalary[i]=scan.nextDouble();
  37. }
  38. }
  39. //Method ReturnAll
  40. private final static void ReturnAll () {
  41. for (int i = 0; i < 99; i++) {
  42. if (EmployeeFirst[i]==null) {
  43. break;
  44. }
  45. else if (EmployeeID[i] >= 0) {
  46. System.out.println("\nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +" " + "Employee ID: "+EmployeeID[i]+" " +"Employee Salary: "+ EmployeeSalary[i]);
  47. }
  48. }
  49. }
  50. //Method ReturnByID
  51. private final static void ReturnByID () {
  52. scan = new Scanner(System.in);
  53. System.out.println("\nEnter the employee ID# for data you wish to retrieve:");
  54. System.out.println("\nEmployee ID#: ");
  55. int EmpSearch = scan.nextInt();
  56. for (int i = 0; i<99; i++) {
  57. if (EmpSearch == EmployeeID [i]) {
  58. System.out.println("\nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +" " + "Employee ID: "+EmployeeID[i]+" " +"Employee Salary: "+ EmployeeSalary[i]);
  59. }
  60. }
  61. }
  62. //Method ReturnBySalary
  63. private final static void ReturnBySalary () {
  64. scan = new Scanner(System.in);
  65. System.out.println("\nEnter the lower salary boundary:");
  66. int LowSal = scan.nextInt();
  67. System.out.println("\nEnter the upper salary boundary");
  68. int HighSal = scan.nextInt();
  69. for (int i = 0; i<99; i++) {
  70. if (EmployeeFirst[i]==null) {
  71. break;
  72. }
  73. else if (EmployeeSalary[i] >= LowSal && EmployeeSalary[i] <= HighSal) {
  74. System.out.println("\nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +" " + "Employee ID: "+EmployeeID[i]+" " +"Employee Salary: "+ EmployeeSalary[i]);
  75. }
  76. }
  77. }
  78. //Main Method
  79. public static void main(String[] args) {
  80. //Call Scanner
  81. Scanner scan = new Scanner(System.in);
  82. //User Welcome Prompt and Menu
  83. System.out.println("Welcome to the Employee Database.");
  84. System.out.println("\nThis program will accept employee names, ID number, and salaries.");
  85. System.out.println("\nThen will return employee information by using Employee ID or salaries by range.");
  86. int MenuSelect;
  87. do {
  88. System.out.println("\n\nMain Menu:");
  89. System.out.println("\n1. Load multiple employees data.");
  90. System.out.println("\n2. Load data for one employee.");
  91. System.out.println("\n3. Return data for all employees.");
  92. System.out.println("\n4. Search and return for employee by employee ID.");
  93. System.out.println("\n5. Search for employees within salary range.");
  94. System.out.println("\n6. Exit Program.");
  95. System.out.println("\nYour selection:");
  96. MenuSelect = scan.nextInt();
  97. if (MenuSelect == 1) {
  98. MultiEmployee();
  99. }
  100. else if (MenuSelect == 2) {
  101. SingleEmployee ();
  102. }
  103. else if (MenuSelect == 3) {
  104. ReturnAll();
  105. }
  106. else if (MenuSelect==4) {
  107. ReturnByID();
  108. }
  109. else if (MenuSelect ==5) {
  110. ReturnBySalary ();
  111. }
  112. else if (MenuSelect == 6) {
  113. System.out.println("\nExit Program...Good-bye.");
  114. break;
  115. }
  116. } while (MenuSelect < 6);
  117. //Scan Close
  118. scan.close();
  119. }
  120. }
toiithl6

toiithl61#

所以问题是,每次插入数组时,都从索引0开始。解决方案是在数组中找到第一个空索引位置,并从该索引开始填充,以便每次添加到新索引时,而不是覆盖已添加的值。为了找到第一个空索引位置,我添加了方法findFirstEquinIndex(),并修改了两个方法multiemployee()和singleemployee(),以使用新添加的方法。

  1. import java.util.Scanner;
  2. public final class ProjectTest {
  3. //Create Method Arrays
  4. final static int [] EmployeeID = new int[99];
  5. final static double [] EmployeeSalary = new double [99];
  6. final static String [] EmployeeFirst = new String [99];
  7. final static String [] EmployeeLast = new String [99];
  8. private static Scanner scan;
  9. public static int findFirstEmptyIndex() {
  10. for(int i = 0;i<EmployeeFirst.length;i++) {
  11. if(EmployeeFirst[i]==null)
  12. return i;
  13. }
  14. return -1;
  15. }
  16. //Method Add MultiEmployee
  17. private final static void MultiEmployee () {
  18. scan = new Scanner(System.in);
  19. System.out.println("\nHow many employees would you like to enter?: ");
  20. int EmployeeCount = scan.nextInt();
  21. int firstEmptyIndex = findFirstEmptyIndex();
  22. for (int i = firstEmptyIndex; i < firstEmptyIndex+ EmployeeCount; i++) {
  23. System.out.println("\nEmployee " + (i+1)+":");
  24. System.out.println("\nEnter employee name (First Last):");
  25. EmployeeFirst[i] = scan.next();
  26. EmployeeLast[i] = scan.next();
  27. System.out.println("\nEnter Employee ID#:");
  28. EmployeeID[i] = scan.nextInt();
  29. System.out.println("\nEnter Employee Salary:");
  30. EmployeeSalary[i]=scan.nextDouble();
  31. }
  32. }
  33. //Method Add SingleEmployee
  34. private final static void SingleEmployee () {
  35. int firstEmptyIndex = findFirstEmptyIndex();
  36. for (int i = firstEmptyIndex; i < firstEmptyIndex + 1 ; i++) {
  37. scan = new Scanner(System.in);
  38. System.out.println("\nEmployee " + (i+1)+":");
  39. System.out.println("\nEnter employee name (First Last):");
  40. EmployeeFirst[i] = scan.next();
  41. EmployeeLast[i] = scan.next();
  42. System.out.println("\nEnter Employee ID#:");
  43. EmployeeID[i] = scan.nextInt();
  44. System.out.println("\nEnter Employee Salary:");
  45. EmployeeSalary[i]=scan.nextDouble();
  46. }
  47. }
  48. //Method ReturnAll
  49. private final static void ReturnAll () {
  50. for (int i = 0; i < 99; i++) {
  51. if (EmployeeFirst[i]==null) {
  52. break;
  53. }
  54. else if (EmployeeID[i] >= 0) {
  55. System.out.println("\nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +" " + "Employee ID: "+EmployeeID[i]+" " +"Employee Salary: "+ EmployeeSalary[i]);
  56. }
  57. }
  58. }
  59. //Method ReturnByID
  60. private final static void ReturnByID () {
  61. scan = new Scanner(System.in);
  62. System.out.println("\nEnter the employee ID# for data you wish to retrieve:");
  63. System.out.println("\nEmployee ID#: ");
  64. int EmpSearch = scan.nextInt();
  65. for (int i = 0; i<99; i++) {
  66. if (EmpSearch == EmployeeID [i]) {
  67. System.out.println("\nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +" " + "Employee ID: "+EmployeeID[i]+" " +"Employee Salary: "+ EmployeeSalary[i]);
  68. }
  69. }
  70. }
  71. //Method ReturnBySalary
  72. private final static void ReturnBySalary () {
  73. scan = new Scanner(System.in);
  74. System.out.println("\nEnter the lower salary boundary:");
  75. int LowSal = scan.nextInt();
  76. System.out.println("\nEnter the upper salary boundary");
  77. int HighSal = scan.nextInt();
  78. for (int i = 0; i<99; i++) {
  79. if (EmployeeFirst[i]==null) {
  80. break;
  81. }
  82. else if (EmployeeSalary[i] >= LowSal && EmployeeSalary[i] <= HighSal) {
  83. System.out.println("\nEmployee Name: " +EmployeeFirst[i] + " " + EmployeeLast[i] +" " + "Employee ID: "+EmployeeID[i]+" " +"Employee Salary: "+ EmployeeSalary[i]);
  84. }
  85. }
  86. }
  87. //Main Method
  88. public static void main(String[] args) {
  89. //Call Scanner
  90. Scanner scan = new Scanner(System.in);
  91. //User Welcome Prompt and Menu
  92. System.out.println("Welcome to the Employee Database.");
  93. System.out.println("\nThis program will accept employee names, ID number, and salaries.");
  94. System.out.println("\nThen will return employee information by using Employee ID or salaries by range.");
  95. int MenuSelect;
  96. do {
  97. System.out.println("\n\nMain Menu:");
  98. System.out.println("\n1. Load multiple employees data.");
  99. System.out.println("\n2. Load data for one employee.");
  100. System.out.println("\n3. Return data for all employees.");
  101. System.out.println("\n4. Search and return for employee by employee ID.");
  102. System.out.println("\n5. Search for employees within salary range.");
  103. System.out.println("\n6. Exit Program.");
  104. System.out.println("\nYour selection:");
  105. MenuSelect = scan.nextInt();
  106. if (MenuSelect == 1) {
  107. MultiEmployee();
  108. }
  109. else if (MenuSelect == 2) {
  110. SingleEmployee ();
  111. }
  112. else if (MenuSelect == 3) {
  113. ReturnAll();
  114. }
  115. else if (MenuSelect==4) {
  116. ReturnByID();
  117. }
  118. else if (MenuSelect ==5) {
  119. ReturnBySalary ();
  120. }
  121. else if (MenuSelect == 6) {
  122. System.out.println("\nExit Program...Good-bye.");
  123. break;
  124. }
  125. } while (MenuSelect < 6);
  126. //Scan Close
  127. scan.close();
  128. }
  129. }
展开查看全部

相关问题