[英]Method does not return anything at all
I wrote a method which gets as an input an array of numbers and an integer (which will be the delimiter) the method will return a 2D array that is sliced according to the delimiter not including the delimiter. 我编写了一个方法,该方法获取一个数字数组和一个整数(将作为定界符)作为输入,该方法将返回一个二维数组,该数组根据不包括定界符的定界符进行切片。 Examples: 例子:
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]]
For some reason when I move my mouse over the name of the method I get the error that my function should return int[][]
由于某种原因,当我将鼠标移到方法的名称上时,出现错误,我的函数应该返回int[][]
This is my code: 这是我的代码:
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 ;
}
This is because result
is defined in the for-loop scope. 这是因为result
是在for循环作用域中定义的。
Please, format your code, and you will see that result
isn't "visible" from return statement. 请格式化代码,您将在return语句中看到result
不是“可见”。
UPDATE: Friendly remainder: you can format selected code in eclipse with a shortkey: Ctrl + Shift + F 更新:友好的余数:您可以使用快捷键在Eclipse中格式化所选代码: Ctrl + Shift + F
Formatted code: 格式化代码:
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 ;
}
You may see that you don't have the method closing }
, and the result is defined in the for-loop
scope, and is returned from there, but I believe that you want to return the result
after the for-loop , isn't it? 您可能会看到没有关闭方法}
,并且结果在for-loop
作用域中定义,并从那里返回,但是我相信您要在for循环 之后返回result
是'是吗?
I suppose that your code should look like this: 我想您的代码应如下所示:
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 ;
}
also, I guess that you have a mistake in the line result[j]=Arrays.copyOfRange(input, firstindex, lastindex);
另外,我猜你在result[j]=Arrays.copyOfRange(input, firstindex, lastindex);
行result[j]=Arrays.copyOfRange(input, firstindex, lastindex);
有一个错误result[j]=Arrays.copyOfRange(input, firstindex, lastindex);
. 。 It's possible that j
will be larger that count
(the size of the result
). j
可能会大于该count
( result
的大小)。 So, you should have another counter or pointer to the last free element in the result
array (destination to copy the next chunk of array). 因此,您应该有另一个计数器或指针指向result
数组中的最后一个空闲元素(复制数组的下一个块的目标)。
UPDATE 3: 更新3:
Well, I don't know how much fair it is, but I did all YOUR work by myself. 好了,我不知道它是多么公平的,但我自己做所有的工作。 Here is the code, which actually works (with comments): 这是实际起作用的代码(带有注释):
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;
}
I run it using the next few lines, and that works just like expected: 我使用接下来的几行来运行它,并且效果与预期的一样:
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();
}
Also, at some point, you may have empty parts (after the split). 另外,在某些时候,您可能有空的零件(拆分后)。 So, I leave it up to you to clean it up (they may appear everywhere(not only at the beginning or the end), if there are two consecutive split-numbers in the input
array). 因此,我留给您清理它(如果input
数组中有两个连续的分割数,它们可能会出现在所有地方(不仅在开头或结尾处)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.