Странный вывод для метода возвращаемого массива

StackOverflow https://stackoverflow.com//questions/20002178

  •  20-12-2019
  •  | 
  •  

Вопрос

Ниже приведена «основная» часть рабочего кода:

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));
.

Другие советы

Как вы сказали, вы печатаете toString() возвращаемого массива.Однако в Java массивы не переопределяют toString(), отсюда и результат, который вы получаете.Вместо того, чтобы просто печатать возвращаемое значение, используйте Arrays.toString(int[]) чтобы напечатать его значение - это должно дать вам желаемый результат.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top