Java 将数组对半、填充,然后重新填充
我有一个算法,我想写的是,取一个数组,把它分成两半,用0填充一半,把数组的剩余部分分成两半,用1填充一半,以此类推,直到所有元素都被填充。由8个字符组成的数组看起来像[0 0 0 1 2 3]。我已经做了很多事情来编写代码,但是总是以一个无休止的循环结束,或者以抛出一个空指针异常结束。我的代码如下。有什么建议吗Java 将数组对半、填充,然后重新填充,java,arrays,Java,Arrays,我有一个算法,我想写的是,取一个数组,把它分成两半,用0填充一半,把数组的剩余部分分成两半,用1填充一半,以此类推,直到所有元素都被填充。由8个字符组成的数组看起来像[0 0 0 1 2 3]。我已经做了很多事情来编写代码,但是总是以一个无休止的循环结束,或者以抛出一个空指针异常结束。我的代码如下。有什么建议吗 public Integer[] generateTestDataHalfs(int size) { //Generate the array I
public Integer[] generateTestDataHalfs(int size)
{
//Generate the array
Integer[] randHalfs = new Integer[size];
//Use an integer to store the location of the array
int arrPlace;
int currNum = 0;
int arrStart = 0; //For the first iteration, start of the array
//Half the size and set arrPlace to size + 1
size /= 2;
arrPlace = size + 1;
//Use a while loop to populate the array
while (size > 1)
{
//Populate part of the array
for (int x = arrStart; x == size; x++)
{
randHalfs[x] = currNum;
}
//Increment the number
currNum++;
//Set it to the next part of the array
size = randHalfs.length - arrPlace;
size /= 2;
arrPlace = size + 1;
arrStart = arrPlace;
}
//Return the new array
return randHalfs;
}
我在代码中看到的第一个问题是
for
循环:终止条件x==size
将计算为false
(并立即终止循环),除非size==arrStart
。您不希望这样,因为这意味着数组的一部分不会被填充。第二个问题是,如果size==1
,则不会初始化(子)数组中的任何内容,这也是错误的。我无法解释无限循环,但可能存在数组长度,size
不会每次通过while
循环更改值的情况。您可以通过使用调试器单步执行或每次通过循环记录变量值来诊断此类情况
在任何情况下,您的代码都可以大大简化。我的版本如下所示:
public Integer[] generateTestDataHalfs(int size) {
Integer [] result = new Integer[size];
Integer fillValue = 0;
int start = 0;
while (start < size) {
int fillLength = (size - start) / 2;
// check for final element
if (fillLength == 0) {
// fill final element
fillLength = 1;
}
for (int i = 0; i < fillLength; i++) {
result[start + i] = fillValue;
}
fillValue++;
start += fillLength;
}
return result;
}
这是我测试的一个解决方案,大小值为整数0-11。我理清了你的逻辑。肯定还有进一步清理的余地。我想把这个留给你做练习
package com.ankur.javacode;
public class DivideArrayHalf {
public static void main(String[] args) {
DivideArrayHalf obj = new DivideArrayHalf();
for(int i: obj.generateTestDataHalfs(0)) {
System.out.println(i);
}
}
public Integer[] generateTestDataHalfs(int size)
{
if (size == 0) {
return null;
}
//Generate the array
Integer[] randHalfs = new Integer[size];
//Use an integer to store the location of the array
int currNum = 0;
int arrStart = 0;
int arrEnd = 0;
int tofill = 0;
tofill = size / 2;
size = size - tofill;
arrStart = arrEnd;
arrEnd = arrStart + tofill;
//Use a while loop to populate the array
while (arrStart != arrEnd)
{
//Populate part of the array
for (int x = arrStart; x < arrEnd; x++)
{
randHalfs[x] = currNum;
}
//Increment the number
currNum++;
tofill = size/2;
size = size - tofill;
arrStart = arrEnd;
arrEnd = arrStart + tofill;
}
randHalfs[arrEnd] = currNum;
//Return the new array
return randHalfs;
}
}
package com.ankur.javacode;
公共类分区{
公共静态void main(字符串[]args){
DivideArrayHalf obj=新的DivideArrayHalf();
对于(int i:obj.generateTestDataHalfs(0)){
系统输出打印LN(i);
}
}
公共整数[]generateTestDataHalfs(整数大小)
{
如果(大小==0){
返回null;
}
//生成数组
整数[]randHalfs=新整数[大小];
//使用整数存储数组的位置
int currNum=0;
int arrStart=0;
int arrEnd=0;
int-tofill=0;
tofill=大小/2;
大小=大小-填充;
arrStart=arrEnd;
arrEnd=arrStart+tofill;
//使用while循环填充数组
while(arrStart!=arrEnd)
{
//填充数组的一部分
用于(int x=arrStart;x
package com.ankur.javacode;
public class DivideArrayHalf {
public static void main(String[] args) {
DivideArrayHalf obj = new DivideArrayHalf();
for(int i: obj.generateTestDataHalfs(0)) {
System.out.println(i);
}
}
public Integer[] generateTestDataHalfs(int size)
{
if (size == 0) {
return null;
}
//Generate the array
Integer[] randHalfs = new Integer[size];
//Use an integer to store the location of the array
int currNum = 0;
int arrStart = 0;
int arrEnd = 0;
int tofill = 0;
tofill = size / 2;
size = size - tofill;
arrStart = arrEnd;
arrEnd = arrStart + tofill;
//Use a while loop to populate the array
while (arrStart != arrEnd)
{
//Populate part of the array
for (int x = arrStart; x < arrEnd; x++)
{
randHalfs[x] = currNum;
}
//Increment the number
currNum++;
tofill = size/2;
size = size - tofill;
arrStart = arrEnd;
arrEnd = arrStart + tofill;
}
randHalfs[arrEnd] = currNum;
//Return the new array
return randHalfs;
}
}