gpt4 book ai didi

java - 返回数组方法的奇怪输出

转载 作者:行者123 更新时间:2023-12-01 17:19:12 24 4
gpt4 key购买 nike

以下是工作代码的“主要”主体:

public class ArrayFunHouse
{
//instance variables and constructors could be used, but are not really needed

//getSum() will return the sum of the numbers from start to stop, not including stop
public static int getSum(int[] numArray, int start, int stop)
{
int sum = 0;

for(int count = start; count <= stop; count++) {
sum += numArray[count];
}
return sum;
}

//getCount() will return number of times val is present
public static int getCount(int[] numArray, int val)
{
int present = 0;

for(int count = 0; count < numArray.length; count++) {
if(numArray[count] == val) {
present++;
}
}
return present;
}

public static int[] removeVal(int[] numArray, int val)
{
int[] removal = new int[numArray.length - getCount(numArray, val)];
int arbitrary = 0;

for(int count = 0; count < removal.length; count++) {
if(numArray[count] == val) {
arbitrary++;
} else {
removal[count - arbitrary] = numArray[count];
}
}

return removal;
}
}

和运行者类:

import java.util.Arrays;

public class ArrayFunHouseRunner
{
public static void main( String args[] )
{
int[] one = {4,10,0,1,7,6,5,3,2,9};

System.out.println(Arrays.toString(one));
System.out.println("sum of spots 3-6 = " + ArrayFunHouse.getSum(one,3,6));
System.out.println("sum of spots 2-9 = " + ArrayFunHouse.getSum(one,2,9));
System.out.println("# of 4s = " + ArrayFunHouse.getCount(one,4));
System.out.println("# of 9s = " + ArrayFunHouse.getCount(one,9));
System.out.println("# of 7s = " + ArrayFunHouse.getCount(one,7));
System.out.println("new array with all 7s removed = "+ ArrayFunHouse.removeVal(one, 7));


}
}

返回一些我期望尝试打印没有 toString 的类时所期望的内容,即:

[4, 10, 0, 1, 7, 6, 5, 3, 2, 9]
sum of spots 3-6 = 19
sum of spots 2-9 = 33
number of 4s = 1
number of 9s = 1
number of 7s = 1
new array with all 7s removed = [I@56f7ce53

我知道我在运行程序中正确调用了它,没有什么可搞砸的,无法查明问题,有什么建议吗?

最终编辑:嗯,这很尴尬。事实上,问题出在运行程序中,而不是调用 Arrays.toString。

最佳答案

打印

System.out.println(Arrays.toString(array));

如果你的数组里面有数组。

System.out.println(Arrays.deepToString(array));

关于java - 返回数组方法的奇怪输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20002178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com