Domanda

Di seguito è riportato il codice "principale" del corpo del lavoro:

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

E la classe Runner:

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


}
}
.

Restituisce qualcosa che mi aspetterei dal tentativo di stampare una classe senza una tostring, I.e. Questo:

[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
.

So che l'ho chiamato correttamente nel corridore, non c'è molto lì per avvitare, non può individuare il problema, qualsiasi consiglio?

Modifica finale: Beh, è imbarazzante.La questione è stata infatti nel corridore, non chiamando un array ..ToString.

È stato utile?

Soluzione

Stampa con

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

E se la tua array ha array all'interno.

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

Altri suggerimenti

Come hai dichiarato, si sta stampando il toString() dell'array restituito.Tuttavia, in Java, gli array non sovrascrivono toString(), quindi l'uscita che stai ottenendo. Invece di stampare il valore restituito, utilizzare Arrays.toString(int[]) per stampare il suo valore - che dovrebbe darti l'uscita desiderata.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top