简体   繁体   English

将2D数组转换为1D数组错误

[英]Convert a 2D array into a 1D array error

I have made a program, but I have a problem and I don't know where but it think its int the convertion. 我已经编写了一个程序,但是有一个问题,我不知道在哪里,但是它认为它是int转换。 I want to convert from 2d to 1d. 我想从2d转换为1d。 So I have this code: 所以我有这段代码:

            System.out.print("\nEnter the employee's (1) Basic Pay (2) Housing Allowance (3) Travel Allowance (example: 4000 500 300): ");
        salary_detail[0][employee_counter] = sc.nextDouble();
        salary_detail[1][employee_counter] = sc.nextDouble();
        salary_detail[2][employee_counter] = sc.nextDouble();
        net_salary[employee_counter]= salary_detail[1][employee_counter]+salary_detail[2][employee_counter]+salary_detail[3][employee_counter];

employee_counter is 0 So first of all I scan the number. employee_counter为0,所以我首先扫描该号码。 But when I want to collect all of this numbers to another array 1d it won't work and I get the error: 但是,当我要将所有这些数字收集到另一个数组1d时,它将无法工作,并且出现错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at project1.Project1.addRecord(Project1.java:90)
at project1.Project1.main(Project1.java:48)

Java Result: 1 Java结果:1

all this is inside a method so how can i fix that ? 所有这些都在方法内部,那么我该如何解决?

try: 尝试:

net_salary[employee_counter]= salary_detail[0][employee_counter]+salary_detail[1][employee_counter]+salary_detail[2][employee_counter];

The first index of an array is 0, so the last will be 2 instead of 3. Since your array has only a length of 3 and you are trying to access the item at index 3 (this is actually the fourth item) you are getting an java.lang.ArrayIndexOutOfBoundsException . 数组的第一个索引是0,所以最后一个将是2,而不是3。由于您的数组只有3的长度,并且您试图访问索引3处的项(实际上是第四个项),因此一个java.lang.ArrayIndexOutOfBoundsException

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

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