Pregunta

El siguiente es el cuerpo "principal" del código de trabajo:

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;
    }
}

Y la clase de corredor:

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


}
}

Devuelve algo que esperaría al intentar imprimir una clase sin toString, es decir.este:

[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

Sé que lo llamé correctamente en el corredor, no hay mucho que arruinar, no puedo identificar el problema, ¿algún consejo?

Edición final:Bueno eso es embarazoso.De hecho, el problema estaba en el corredor, no en llamar a Arrays.toString.

¿Fue útil?

Solución

imprimir con

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

y si su matriz tiene matriz dentro.

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

Otros consejos

Como dijiste, estás imprimiendo el toString() de la matriz devuelta.Sin embargo, en Java, las matrices no anulan toString(), de ahí el resultado que estás obteniendo.En lugar de simplemente imprimir el valor devuelto, use Arrays.toString(int[]) para imprimir su valor, eso debería darle el resultado deseado.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top