2013-11-15 73 views
0

以下奇怪的輸出的工作做的代碼「主」體:退貨陣列方法

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。

+2

你打算如何打印這個值? – broncoAbierto

+0

向我們展示您調用該方法並使用其返回值的代碼。你可能會認爲你並沒有把這部分搞砸,但所有的證據都表明你實際上已經這樣做了。 –

+1

使用'Arrays.toString(array)' – Admit

回答

4

打印與

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

,如果你的陣列有內部數組。

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

如上所述,您正在打印返回數組的toString()。但是,在Java中,數組不會覆蓋toString(),因此您獲得的輸出。 而不是僅僅打印返回的值,使用Arrays.toString(int[])打印它的值 - 應該給你想要的輸出。