0

I have this code and when I run it I get this: [I@2c7b84de.

Can someone explain to me why I get that?

public class EJer22 {
    public static void main(String[] args){
        int[] numeros = { 50, 21, 6, 97, 18 };
        System.out.println(numeros);

    }
    public static int[] pruebas (int[] P){
        int[] S = new int[P.length];
        if(P.length < 10){
            System.out.println("Hay mas de 10 numeros");
        } else {
            for(int i = 0; i < P.length; i++){
                //S = P[i];
                if(10 >= P[i]){
                    S[i] = -1;
                    
                }else{
                    S[i] = P[i];
                }
            }
        }
        return S;
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rai1233
  • 3
  • 1

2 Answers2

0

You get this because of that:

System.out.println(numeros);

It‘s the String representation of an integer array.

Christoph Dahlen
  • 826
  • 3
  • 15
0

numeros is an int array, which does not automatically serialise to a String. You are seeing the object reference value, rather than the contents.

If you convert it to a List, you will see the individual elements as the toString() method of List will do this for you

public static void main(String[] args){
    List<Integer> numeros = Arrays.asList(50, 21, 6, 97, 18 );
    System.out.println(numeros);
}

Or, use Arrays.toString(numeros) to avoid using a List.

The pruebas method is never called but I guess that is not relevant to the question you've asked?

Andrew B
  • 69
  • 3
  • I have already changed it because I had forgotten to call the method but the same thing still comes out, why? public static void main(String[] args){ int[] numeros = { 50, 21, 6, 97, 18 }; int[] pruebas2 = pruebas(numeros); System.out.println(pruebas2); } – Rai1233 Dec 03 '22 at 09:58
  • Same reason - it is printing the array object reference. – Andrew B Dec 03 '22 at 10:02