我编写了一个方法,该方法获取一个数字数组和一个整数(将作为分隔符)作为输入,该方法将返回一个根据分隔符(不包括分隔符)进行切片的二维数组。 示例:
splitArrayNyNum([0, 0, 0, 3, 1, 1, 1], 3) -> [[0, 0, 0], [1, 1, 1]]
splitArrayNyNum([1, 2, 3, 1, 2, 3, 1, 1, 2, 2, 3, 1], 3) -> [[1, 2], [1, 2], [1, 1, 2, 2], [1]]
splitArrayNyNum([3 , 1 ,3 ,3], 3) -> [[1]]
出于某种原因,当我将鼠标移到方法名称上时,我收到错误消息,指出我的函数应返回 int[][]
这是我的代码:
public static int[][] splitArrayByNum(int[] input, int number){
if (input[0]==number)
for ( int i = 0 ; i < input.length - 1 ; i++ )
{
input[ i ] = input[ i + 1 ] ;
}
if ((input[(input.length)-1])==number)
for ( int i = (input.length)-1 ; i < input.length - 1 ; i++ )
{
input[ i ] = input[ i + 1 ] ;
}
int count = 0;
for (int i = 0; i < input.length; i++) {
if (input[i] == number) {
count++;
}
int[][] result = new int[count][];
int firstindex=0;
int lastindex=0;
for (int j=0; j<input.length; j++){
if (input[j]==number){
result[j]=Arrays.copyOfRange(input, firstindex, lastindex);
firstindex=lastindex=j;
}
lastindex++ ;
}
return result ;
}
最佳答案
这是因为 result
是在 for 循环范围内定义的。
请格式化您的代码,您将看到 结果
在 return 语句中不“可见”。
更新:友好的剩余部分:您可以使用快捷键在 eclipse 中格式化所选代码:Ctrl + Shift + F
格式化代码:
public static int[][] splitArrayByNum(int[] input, int number){
if (input[0]==number)
for ( int i = 0 ; i < input.length - 1 ; i++ )
{
input[ i ] = input[ i + 1 ] ;
}
if ((input[(input.length)-1])==number)
for ( int i = (input.length)-1 ; i < input.length - 1 ; i++ )
{
input[ i ] = input[ i + 1 ] ;
}
int count = 0;
for (int i = 0; i < input.length; i++) {
if (input[i] == number) {
count++;
}
int[][] result = new int[count][];
int firstindex=0;
int lastindex=0;
for (int j=0; j<input.length; j++){
if (input[j]==number){
result[j]=Arrays.copyOfRange(input, firstindex, lastindex);
firstindex=lastindex=j;
}
lastindex++ ;
}
return result ;
}
您可能会发现您没有关闭 }
的方法,并且结果是在 for-loop
作用域中定义的,并从那里返回,但我相信您希望在 for-loop 之后返回结果
,不是吗?
我想你的代码应该是这样的:
public static int[][] splitArrayByNum(int[] input, int number){
if (input[0]==number)
for ( int i = 0 ; i < input.length - 1 ; i++ )
{
input[ i ] = input[ i + 1 ] ;
}
if ((input[(input.length)-1])==number)
for ( int i = (input.length)-1 ; i < input.length - 1 ; i++ )
{
input[ i ] = input[ i + 1 ] ;
}
int count = 0;
for (int i = 0; i < input.length; i++) /*removed bracket here*/
if (input[i] == number) {
count++;
}
int[][] result = new int[count][];
int firstindex=0;
int lastindex=0;
for (int j=0; j<input.length; j++){
if (input[j]==number){
result[j]=Arrays.copyOfRange(input, firstindex, lastindex);
firstindex=lastindex=j;
}
lastindex++ ;
}
return result ;
}
另外,我猜您在 result[j]=Arrays.copyOfRange(input,firstindex,lastindex);
行中有一个错误。 j
可能会大于 count
(结果
的大小)。因此,您应该有另一个计数器或指针,指向 result
数组中最后一个空闲元素(复制下一个数组 block 的目标)。
更新3:
嗯,我不知道这有多公平,但我自己完成了你的所有工作。 这是实际有效的代码(带注释):
public static int[][] splitArrayByNum(int[] input, int number) {
if(input.length == 0) {
return new int[0][];
}
int count = 0;
for (int i = 0; i < input.length; i++) {
if (input[i] == number) {
count++;
}
}
if(input[input.length - 1] != number) {
/*we need to add the end of the array manually*/
count ++;
}
int[][] result = new int[count][];
int firstIndex = 0;
int iter = 0;
for (int j = 0; j < input.length; j++) {
if (input[j] == number) {
result[iter++] = Arrays.copyOfRange(input, firstIndex, j);
firstIndex = j+1;
}
}
if(input[input.length - 1] != number) {
/*manually adding the end of the array*/
result[count-1] = Arrays.copyOfRange(input, firstIndex, input.length);
}
return result;
}
我使用接下来的几行运行它,效果就像预期的那样:
int[] inp = new int[]{1, 3, 3, 5};
int[][] sp = splitArrayByNum(inp, 3);
for(int i=0;i<sp.length;i++) {
System.out.print(i + ": ");
for(int j=0;j<sp[i].length;j++) {
System.out.print(sp[i][j] + " ");
}
System.out.println();
}
此外,在某些时候,您可能会有空的部分(在拆分之后)。因此,我将其留给您来清理(如果 input
数组中有两个连续的分割数字,它们可能会出现在任何地方(不仅在开头或结尾)。
关于java - 方法根本不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27013869/