Pregunta

I have a problem with my code-this metod returns a tab of ints:

int[] czynnikiPierwsze(int n){
    dzielniki=new int[20];
    for(int j=0;j<n;j++){
    for(int i=0;i<n;i++){
        if(n%tab[i]==0){
            dzielniki[j]=tab[i];
            n=n/tab[i];
            break;
        }
    }
    }
    return dzielniki;
}

There are for example {2,2,3,8,0,0,0,0,0} in this tab(dzielniki). How can i use this tab in main and print those numbers ? I created new object and used that method:

RozkladLiczby a=new RozkladLiczby(100);
    System.out.println(a.czynnikiPierwsze(69));

And i get werid output: [I@1693b52b - Probably because i want to print the whole tab at once, how can i print it property ?

¿Fue útil?

Solución

An array of ints is an object too, but that "class" doesn't override the toString method from Object, which is responsible for the "weird output" you see:

In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Print the array using Arrays.toString instead.

System.out.println(Arrays.toString(a.czynnikiPierwsze(69)));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top