char和int多种情况下的转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.gxnu.bigdata.study.datastyle;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import org.junit.Test;

public class {

public void testArray() throws IOException{
List<String> allLines = Files.readAllLines(Paths.get(
"D:",
"MyEclipse 2016",
"Workspaces",
"7-12",
"src",
"com",
"maoge",
"study",
"io",
"IoEx.java"),
Charset.forName("UTF-8"));

int[] arr = new int[65536];

allLines.forEach((s)->{
for(char ch:s.toCharArray()){

//System.out.println(arr[ch-1]);
//System.out.println(arr[ch-1]+1);
//将某字符对应的数字减1作为该字符的角标,对该字符进行累计统计出现的次数。
arr[ch-1] = arr[ch-1]+1;

}
});

for(int i=0;i<arr.length;i++){
char ch = (char)(i+1); //将数字还原成字符本身
if(arr[i]>0){
System.out.println(ch+"出现了"+arr[i]+"次");
}
}


}
}

limaodeng

scribble