简体   繁体   English

构造函数的字符串输入参数上的 Java 越界异常

[英]Java Out of Bounds exception on String input parameter for Constructor

I'm new to Java and I keep trying to debug my code to no avail.我是 Java 新手,我一直试图调试我的代码,但无济于事。 I'm trying to use arrayList to build an array for a newEmployee and append it an arraylist of employees ( ArrayList> ).我正在尝试使用 arrayList 为 newEmployee 构建一个数组并将其附加到一个员工数组列表( ArrayList> )。 The dateParse, nameParse and addressParse are separate methods being called within this array method. dateParse、nameParse 和 addressParse 是在此数组方法中调用的单独方法。

Where error is:错误在哪里:

import java.util.ArrayList;

public class NewEmployee {

   private ArrayList<ArrayList<String>> allEmployees;
   private ArrayList<String> currentEmployee;


   //Constructor
   NewEmployee(String number, String name, String address, String date) {
      //grab first field, Number
      this.currentEmployee.set(0, number);
      //grab next field, full Name and pass to nameParse as input
      NameParse nameParse = new NameParse(name);
      this.currentEmployee.set(1 , nameParse.getFirstName());
      this.currentEmployee.set(2, nameParse.getLastName());
      //call to Address Parse to separate address fields
      AddressParse addressParse = new AddressParse(address);
      this.currentEmployee.set(3, addressParse.getStreet());
      this.currentEmployee.set(4, addressParse.getCity());
      this.currentEmployee.set(5, addressParse.getStateCode());
      this.currentEmployee.set(6, addressParse.getPostalCode());
      //grab last input field, Hire Date & sent to DateParse
      DateParse dateParse = new DateParse(date);
      this.currentEmployee.set(7, dateParse.getMonth());
      this.currentEmployee.set(8, dateParse.getDay());
      this.currentEmployee.set(9, dateParse.getYear());
      //add the current employee to a row in allEmployees
      this.allEmployees.add(this.currentEmployee);
   }

   //default constructor
   NewEmployee(){
      this.currentEmployee = new ArrayList<String>();
      //this.allEmployees = new ArrayList<ArrayList<String>>();
   }

   public String[][] toArray() {
      //10 fields for each employee
      String [][] everyEmployee = new String[this.allEmployees.size()][10];
      return everyEmployee;
   }


   public String toString() {
    //create temporary string of values
      StringBuilder tempEmployee = new StringBuilder();
      for (int i=0; i<this.allEmployees.size()-1; i++) {
         //String [] empDetails = new String[allEmployees(i).size]
         for(int j = 0; j<10; j++) {
            //insert arraylist values, elementwise into string
            tempEmployee = tempEmployee.append(this.allEmployees.get(i).get(j));
         }
      }
      //return the string
      return tempEmployee.toString();
   }


   public ArrayList<ArrayList<String>> getAllEmployees() {
      return this.allEmployees;
   }

      public ArrayList<String> getCurrentEmployee() {
      return this.currentEmployee;
   }
}

From main, the variables are input correctly and sent within specifications, but I get an error at从 main 开始,变量输入正确并在规范内发送,但我在

this.currentEmployee.set(0, number);

If anyone can help understand how to make this work or why it's not working that would be amazing!如果有人能帮助理解如何使这项工作或为什么它不起作用,那就太棒了!

If this can help anyone else-- I never got this code working.如果这可以帮助其他任何人 - 我从来没有让这段代码工作。 I tried using .add as the comment suggested and it didn't work, still with the same error.我尝试使用 .add 作为评论建议,但没有奏效,仍然出现相同的错误。 I also tried allocating 30 at the ArrayList level as well as using ensurecapacity code.我还尝试在 ArrayList 级别分配 30 以及使用 ensurecapacity 代码。

The thing that ended up working was: I redesigned the Employee class to hold attributes, no list or array.最终工作的事情是:我重新设计了 Employee 类来保存属性,没有列表或数组。

Then I made a separate file CurrentEmployeeArray class that held all of this information in an array.然后我创建了一个单独的文件 CurrentEmployeeArray 类,将所有这些信息保存在一个数组中。

In yet another file I put the AllEmpArrayList class that concatenated all the Current Employees together using the .add .在另一个文件中,我放置了 AllEmpArrayList 类,该类使用 .add 将所有当前员工连接在一起。

I don't know why none of the other solutions worked- all theoretically should've done so.我不知道为什么其他解决方案都不起作用 - 理论上都应该这样做。 However, the design is more robust when you separate all these tasks too.但是,当您也将所有这些任务分开时,设计会更加稳健。 As I go forward I'm learning that, though from my viewpoint a lot of "simple" classes makes things more complex than putting everything into an array at once, a lot of simple classes helps a ton when it comes to debugging, compiling, and running Java programs.随着我的前进,我了解到,尽管从我的角度来看,许多“简单”类使事情比一次将所有内容放入数组更复杂,但许多简单的类在调试、编译、和运行 Java 程序。 I don't know if this will hold true over time- but it is for now.我不知道随着时间的推移这是否会成立——但现在是这样。

Employee:员工:

/**
 * this method takes a String number, String name, String address, and String 
 * date and sends them to other methods to separate fields and 
 * creates a new employee array.  Public methods include getters, setters, and 
 * toString.
 * @author CaseyJayne
 *
 */
public class NewEmployee {
   private String employeeCode;
   private String firstName;
   private String lastName;
   private String streetAddress;
   private String city;
   private String stateCode;
   private String zipCode;
   private String hireMonth;
   private String hireDay;
   private String hireYear;
   private String [] currentEmployee = new String [10];


   //Constructor
   NewEmployee(String number, String name, String address, String date) {
      setEmployeeCode(number);
      NameParse nameParse = new NameParse(name);
      setFirstName(nameParse.getFirstName());
      setLastName(nameParse.getLastName());
      AddressParse addressParse = new AddressParse(address);
      setStreetAddress(addressParse.getStreet());
      setCity(addressParse.getCity());
      setStateCode(addressParse.getStateCode());
      setZipCode(addressParse.getPostalCode());
      DateParse dateParse = new DateParse(date);
      setHireMonth(dateParse.getMonth());
      setHireDay(dateParse.getDay());
      setHireYear(dateParse.getYear());
   }
}

ArrayList Class ArrayList 类

class AllEmployeesArray {
   private ArrayList<String[]> allEmployeesList= new ArrayList<>();

   AllEmployeesArray(String[] inputArray){
      this.allEmployeesList.add(inputArray);
   }
   //default constructor
   AllEmployeesArray(){
      this.allEmployeesList.ensureCapacity(20);
   }

public void addEmp(String[] inputArray){
      this.allEmployeesList.add(inputArray);
   }
   public ArrayList<String[]> getAllEmployeesList() {
      return this.allEmployeesList;
   }

within main:主要内容:

String[] newEmpArray= {newEmp.getEmployeeCode(), newEmp.getFirstName(), 
               newEmp.getLastName(), newEmp.getHireDay(), newEmp.getHireMonth(), 
               newEmp.getHireYear(), newEmp.getStreetAddress(), 
               newEmp.getCity(), newEmp.getStateCode(), newEmp.getZipCode()};
         allEmpArray.addEmp(newEmpArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM