以下奇怪的輸出的工作做的代碼「主」體:退貨陣列方法
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 = [[email protected]
我知道,我把它叫做正確在賽跑者中,沒有太多的東西可以搞砸,不能指出問題,有什麼建議?
最終編輯:那真是令人尷尬。問題實際上是在跑步者中,而不是調用Arrays.toString。
你打算如何打印這個值? – broncoAbierto
向我們展示您調用該方法並使用其返回值的代碼。你可能會認爲你並沒有把這部分搞砸,但所有的證據都表明你實際上已經這樣做了。 –
使用'Arrays.toString(array)' – Admit