Java基础-数据类型应用案例展示

                  Java基础-数据类型应用案例展示

                                    作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.把long数据转换成字节数组,把字节数组数据转换成long。

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 package cn.org.yinzhengjie.smallTestBullKnife;
 7 
 8 /**
 9  *     1.把long数据转换成字节数组.
10  *
11  *     2.把字节数组数据转换成long.
12  */
13 
14 public class GetLongBytesDemo {
15     public static void main(String[] args) {
16         long src = -1000;
17         System.out.println(src);
18         byte[] arr = longTobytes(src);
19         for (byte b : arr) {
20             System.out.printf("%d,",b);
21         }
22         System.out.println();
23         long dest = bytesToLong(arr);
24         System.out.println(dest);
25     }
26 
27     public static byte[] longTobytes(long number){
28         byte[] bys = new byte[8];
29         bys[0] = (byte)(number >> 56);
30         bys[1] = (byte)(number >> 48);
31         bys[2] = (byte)(number >> 40);
32         bys[3] = (byte)(number >> 32);
33         bys[4] = (byte)(number >> 24);
34         bys[5] = (byte)(number >> 16);
35         bys[6] = (byte)(number >> 8);
36         bys[7] = (byte)(number >> 0);
37         return bys;
38     }
39 
40     public static long bytesToLong(byte[] arr){
41         long number;
42         long lon1 = (arr[0] & (long)0xFF) << 56;
43         long lon2 = (arr[1] & (long)0xFF) << 48;
44         long lon3 = (arr[2] & (long)0xFF) << 40;
45         long lon4 = (arr[3] & (long)0xFF) << 32;
46         long lon5 = (arr[4] & (long)0xFF) << 24;
47         long lon6 = (arr[5] & (long)0xFF) << 16;
48         long lon7 = (arr[6] & (long)0xFF) << 8;
49         long lon8 = (arr[7] & (long)0xFF) << 0;
50         //number = lon1 + lon2 +lon3 + lon4 +lon5 + lon6 +lon7 + lon8;      //方法可行且便于理解,但不推荐使用
51         number = lon1 | lon2 |lon3 | lon4 | lon5 | lon6 | lon7 | lon8;      //位运算是最快的,加减乘除最终都会转换成位运算。虽然不容易理解,但是推荐使用!
52         return number;
53     }
54 }
55 
56 /*
57 以上代码执行结果如下:
58 -1000
59 -1,-1,-1,-1,-1,-1,-4,24,
60 -1000
61  */

二.有5亿整数(非负),去重计算不同整数的个数,300M内存。

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 package cn.org.yinzhengjie.smallTestBullKnife;
 7 
 8 import org.junit.Test;
 9 
10 public class IntegerDeweighting {
11     /**
12      * 5亿整数去重统计,这里只是写一个思想!
13      */
14     @Test
15     public void test5Billion(){
16         int len = 0;
17         if (Integer.MAX_VALUE % 8 == 0) {
18             len = Integer.MAX_VALUE / 8;
19         } else {
20             len = Integer.MAX_VALUE / 8 + 1;
21         }
22         //初始化字节数组
23         byte[] bytes = new byte[len];
24         //定义需要去重的数据,我们将这些测试数据定义到一个数组中!
25         byte[] arr = {1,2,3,4,5,6,7,1,2,3,4,5,8,10};
26         for (byte b : arr) {
27             do5Billion(bytes,b);
28         }
29         System.out.println(count1(bytes));
30     }
31 
32     /**
33      * 将正整数存在数组中的位置中
34      * @param bytes : 存储正整数的数组
35      * @param temp  : 需要存储的正整数的值
36      */
37     public static void do5Billion(byte[] bytes , int temp){
38         //计算数组下标
39         int index = temp / 8 ;
40         //计算位的坐标
41         int bit = temp % 8 ;
42         //存放1到对应的坐标上去,如果下表中存储的数据已经满了,那么就不需要进行或运算了!
43         if(bytes[index] != -1){
44             bytes[index] = (byte)(bytes[index] | (1 << bit)) ;
45         }
46     }
47 
48     /**
49      * 用于统计正整数出现的个数。
50      * @param bytes : 存储数组的数组
51      * @return      : 返回正整数出现的次数
52      */
53     public static int count1(byte[] bytes){
54         //统计正数出现的个数
55         int count = 0 ;
56         //遍历数组
57         for(byte b : bytes){
58             //遍历每个数组的字节数组的值,如果字节数组中下标存储的值为1,那么就让count加1.
59             for(int i = 0 ; i < 8 ; i ++){
60                 count = count + ((b >> i) & 0x1) ;
61             }
62         }
63         return count ;
64     }
65 }
66 
67 /*
68 以上代码执行结果如下:
69 9
70 */

三.通过程序创建文本文件,内容是abc,采用uncode码,文件大小是10字节。

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 package cn.org.yinzhengjie.smallTestBullKnife;
 7 
 8 import org.junit.Test;
 9 
10 import java.io.FileOutputStream;
11 
12 /**
13  * 通过程序创建文本文件,内容是abc,采用uncode码,文件大小是10字节
14  */
15 public class UnicodeFile {
16     private final String filePath = "D:\BigData\JavaSE\yinzhengjieData\yinzhengjie.txt";
17     @Test
18     public void touchFile() throws Exception {
19         FileOutputStream fos = new FileOutputStream(filePath);
20         //写入Unicode的头部信息,需要写入连续的-2和-1,占用2个字节。要注意的是UTF8或者GBK等编码都没有这个编码头部信息哟!
21         fos.write(-2);
22         fos.write(-1);
23         //我们知道Unicode编码在存储字符时是需要用2个字节存储。此时我们写一个字符‘a’,它用两个字节表示则为:"0,97",因此用Unicode写入'a'如下:
24         fos.write(0);
25         fos.write(97);
26         //此时我们再一次写入头部信息
27         fos.write(-2);
28         fos.write(-1);
29         //这个时候我们写入连续写入‘b’,'c'
30         fos.write(0);
31         fos.write(98);
32         fos.write(0);
33         fos.write(99);
34         //释放资源
35         fos.close();
36     }
37 }

  生产Unicode编码的文件如下:

四.将byte变换成无符号的整数(0 ~ 255 , 正数不变)

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 package cn.org.yinzhengjie.smallTestBullKnife;
 7 
 8 import org.junit.Test;
 9 
10 public class UnsignedIntegers {
11     @Test
12     public void myUnsignedNumber(){
13         byte num = -1;
14         int i = num & 0xFF;
15         System.out.println(i);
16     }
17 }
18 
19 /*
20 以上代码执行结果如下:
21 255
22  */
原文地址:https://www.cnblogs.com/yinzhengjie/p/9255584.html