我想递归地遍历2D数组并每次替换一个内部数组。问题在于似乎未创建任何数组。尝试访问数组中的值会使Java抱怨使用的索引超出范围。
如何使用递归替换2D数组中的内部数组?
我必须动态替换阵列,因为我希望2D阵列参差不齐。每个索引应包含一个数组,该数组的长度等于其索引号+ 1。
我如何调用我的方法:
double[][] personArr = new double[5][];
personArr = personArrCreator(personArr, 0);
我的方法:
/**
* Creates a ragged, pyramid-shaped, 2d array
* @param pArr The blank 2d Array to fill out
* @param num Controls the base case.
* @return double[][] The finished 2d array
*/
private static double[][] personArrCreator(double[][] pArr, int num) {
pArr[num] = new double[num];
if (num == 4) {
return pArr;
}
personArrCreator(pArr, num + 1);
return null; // never called
}
答案 0 :(得分:2)
return null; // never called
这实际上被调用,一旦达到基本情况,所有其他方法将返回null。相反,您想通过解决递归调用返回到Array
的内容来返回方法的结果:
return personArrCreator(pArr, num + 1);
哪个会返回:
[[], [0.0], [0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]
这将返回大小为零的Array
。我不认为这是您想要的。相反,您可以将大小初始化为num + 1
来解决此问题:
private static double[][] personArrCreator(double[][] pArr, int num) {
pArr[num] = new double[num + 1];
if (num == 4) {
return pArr;
}
return personArrCreator(pArr, num + 1);
}
哪个产生Array
:
[[0.0], [0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]]
答案 1 :(得分:1)
public class EckEmployee2 {
private int rank;
private double number;
private double salary;
private String name;
private String department;
private String position;
public EckEmployee2() {
number = 0;
name = null;
department = null;
position = null;
salary = 0;
rank = 0;
}
public EckEmployee2(double number, String name) {
this.number = number;
this.name = name;
}
public EckEmployee2(double number, String name, String department, String position, double salary, int rank) {
this.number = number;
this.name = name;
this.department = department;
this.position = position;
this.salary = salary;
this.rank = rank;
}
public void setNumber(double num) {
this.number = num;
}
public double getNumber() {
return this.number;
}
public void setName(String nam) {
this.name = nam;
}
public String getName() {
return this.name;
}
public void setDepartment(String dept) {
this.department = dept;
}
public String getDepartment() {
return this.department;
}
public void setPosition(String pos) {
this.position = pos;
}
public String getPosition() {
return this.position;
}
public void setSalary(double sal) {
this.salary = sal;
}
public double getSalary() {
return this.salary;
}
public void setRank(int ran) {
this.rank = ran;
}
public int getRank() {
return this.rank;
}
public boolean checkBonus() {
boolean bonus = false;
if (rank < 5) {
bonus = false;
} else if (rank >= 5)
bonus = true;
return bonus;
}
public void displayEmployee() {
if (checkBonus() == true) {
System.out.println("-------------------------- ");
System.out.println("Name: " + name);
System.out.printf("Employee Number: %09.0f\n" , number, "\n");
System.out.println("Department: \n" + department);
System.out.println("Position: \n" + position);
System.out.printf("Salary: %,.2\n" , salary);
System.out.println("Rank: \n" + rank);
System.out.printf("Bonus: $\n", 1000);
System.out.println("-------------------------- ");
} else if (checkBonus() == false)
System.out.println("--------------------------");
System.out.println("Name: " + name);
System.out.printf("Employee Number: %09.0f\n" , number, "\n");
System.out.println("Department: " + department);
System.out.println("Position: " + position);
System.out.printf("Salary: %,.2f\n" , salary);
System.out.println("Rank: " + rank);
System.out.println("-------------------------- ");
}
}
解析为null,因为personArr = personArrCreator(personArr, 0);
不正确,实际上,当您递归递归堆栈时,它将被调用4次。
在return null; // never called
返回personArrCreator(pArr, 4);
之后,对此没有任何反应,而是返回了pArr
。这是因为null
返回将返回上一个递归调用,而不是初始函数调用。
如果您要递归执行此操作(也可以循环执行此操作),请尝试
personArrCreator(pArr, 4);