首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回数组方法的奇怪输出

返回数组方法的奇怪输出
EN

Stack Overflow用户
提问于 2013-11-15 13:26:47
回答 2查看 3.7K关注 0票数 0

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

代码语言:javascript
复制
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;
    }
}

跑步班:

代码语言:javascript
复制
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的情况下尝试打印类时所期望的内容,即:

代码语言:javascript
复制
[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。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-15 13:29:08

印刷用

代码语言:javascript
复制
System.out.println(Arrays.toString(array));

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

代码语言:javascript
复制
System.out.println(Arrays.deepToString(array));
票数 4
EN

Stack Overflow用户

发布于 2013-11-15 13:30:01

如前所述,您正在打印返回数组的toString()。但是,在Java中,数组不覆盖toString(),因此得到了输出。而不是只打印返回的值,使用Arrays.toString(int[])打印它的值-这应该给你想要的输出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20002178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档