java - BubbleSort -my code returns random addresses -


i have implemented code bubblesort algorithm returns weird error please tell me problem is?

public class bubblesort {      public static int[] unsorted = new int[5];      public void assignrandom(){         for(int = 0; < unsorted.length; i++){             unsorted[i] = (int)(math.random()*10) + 10;         }     }      public void swapvalues(int first, int second){          int temp = unsorted[first];         unsorted[first] = unsorted[second];         unsorted[second] = temp;     }      public void bubblesort() {          for(int = unsorted.length - 1; >= 0; i--){             for(int j = 0; j < i; j++){                 if(unsorted[j] > unsorted[j+1])                     swapvalues(j,j+1);                 }             }     system.out.print(unsorted);     }      public static void main(string[] args){          bubblesort newbubble = new bubblesort();         newbubble.assignrandom();         newbubble.bubblesort();    } } 

this code bubblesort (assignmrandom assigning random value array , sort)

it returns: [i@1658fe12

that's not random address. that's tostring representation of int[]:

[i@1658fe12 

[ means array, i means of integers, , 1658fe12 hashcode of array. representation documentation:

getclass().getname() + '@' + integer.tohexstring(hashcode())

and, class.getname:

if class object represents class of arrays, internal form of name consists of name of element type preceded 1 or more '[' characters representing depth of array nesting. encoding of element type names follows:

element type: int, encoding: i

the hashcode of array identity hashcode inherited object approximately representation of memory location of array (not exactly; details, details).

if want print array, have say

for(int = 0; < unsorted.length; i++) {     system.out.println(unsorted[i]); } 

or

system.out.println(arrays.tostring(unsorted)); 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -