Étrange de sortie pour le retour de la matrice de la méthode
Question
Le suivant est le "principal" corps de travail-faire code:
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;
}
}
Et le coureur de classe:
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));
}
}
Retourne quelque chose que je m'attends de toute tentative d'impression d'une classe sans toString, c'est à direce:
[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
Je sais que je l'ai appelé correctement dans la roue, il n'y a pas grand-chose, à visser, ne peut pas identifier le problème, un conseil?
Montage Final:Eh bien, c'est embarrassant.La question était en fait de la roue, de ne pas appeler un des Tableaux.toString.
La solution
Imprimer avec
System.out.println(Arrays.toString(array));
et si votre tableau a une matrice à l'intérieur.
System.out.println(Arrays.deepToString(array));
Autres conseils
Comme vous l'avez dit, vous avez l'impression de l' toString()
du tableau retourné.Toutefois, en Java, les tableaux ne pas remplacer toString()
, donc la sortie que vous obtenez.Au lieu de juste l'impression de la valeur retournée, l'utilisation Arrays.toString(int[])
pour imprimer, c'est la valeur qui devrait vous donner le résultat souhaité.