How to print an int array returned in a method in the main function , Java -
this question has answer here:
- what's simplest way print java array? 24 answers
first of all,i newbie in java , came across issue several times. trying return number of occurrences of char in string , implemented method called countcharacters looks this:
public static int[] countcharacters(string s) { string alphabet="abcdefghijklmnopqrstuvwxyz"; int[] sircounter=new int[alphabet.length()]; for(int i=0;i<sircounter.length;i++) { sircounter[i]=0; } for(int j=0;j<alphabet.length();j++) { for(int i=0;i<s.length();i++) { if(alphabet.charat(j)==(s.charat(i))) { sircounter[j]++; } } } return sircounter; }
in other words, each time character alphabet found in string ,the argument of method, value 0 each position sircounter incremented number of occurrence. wanted print array position position didn't work , if in main method:
string text="hannah"; system.out.println(class.countcharacters(text));
i hex code: [i@659e0bfd don't understand. me? many , have nice day!
this happening because array treated object in java.and when try print object in java hash code(as long object hasn't have tostring() defined!)
try rather-
string text="hannah"; int counter[]=class.countcharacters(text); for(int i=0;i<counter.length;i++){ system.out.print(counter[i]+" "); }
Comments
Post a Comment