主要是利用了字符的ascii码。以ascii码作为索引。
代码:
展开
1 import java.util.Scanner; 2 public class FindMostChar { 3 4 /** 5 * @author 王锦 6 * @Email jinksw@vip.qq.com 7 * @param args 8 */ 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 Scanner s = new Scanner(System.in); 12 String str = s.nextLine(); 13 showMostCharAndNum(str); 14 } 15 16 public static void showMostCharAndNum(String str) 17 { 18 int[] asciis = new int[256]; 19 int maxIndexAndChar = 0; 20 for(int i = 0; i < str.length();++i) 21 { 22 asciis[str.charAt(i)]++; 23 if(asciis[str.charAt(i)] > maxIndexAndChar) 24 maxIndexAndChar = str.charAt(i); 25 } 26 System.out.println((char)maxIndexAndChar + " " + asciis[maxIndexAndChar]); 27 } 28 }