[英]Java Array returning as two-dimensional
I'm trying to do a Kata on Codewars in Java that asks "you have decided to write a function that will return the first n elements of the sequence with the given common difference step and first element first ." 我试图做一个Codewars卡塔在Java中,询问“你决定写,将返回给定公差步和第一个元素的第一序列的第n个元素的功能。” Below I have done what is asked, but when I return the final array it returns as two-dimensional (like [[1, 2, 3, 4]] instead of [1, 2, 3, 4]). 在下面,我完成了所要求的操作,但是当我返回最终数组时,它以二维形式返回(例如[[1、2、3、4]]而不是[1、2、3、4])。 Please help me understand why and how to fix this! 请帮助我了解为什么以及如何解决此问题!
import java.util.Arrays;
class Progression {
public static String arithmeticSequenceElements(int first, int step,
long total) {
int[] intArr;
intArr = new int [(int)total];
intArr[0] = first;
int i = 1;
while(i<total){
intArr[i] = intArr[i-1] + step;
i++;
}
return Arrays.toString(intArr);
}
}
The platform is expecting you to return just the values 1, 2, 3, 4
. 该平台希望您仅返回值1, 2, 3, 4
。
You're returning the default Arrays.toString()
representation which is [1, 2, 3, 4]
. 您将返回默认的Arrays.toString()
表示形式,即[1, 2, 3, 4]
。 The platform then displays it as <[[1, 2, 3, 4]]>
. 然后平台将其显示为<[[1, 2, 3, 4]]>
。
You could return just the elements with for example 您可以使用例如返回仅元素
String str = Arrays.toString(intArr);
return str.substring(1, str.length()-1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.