1.

/*
				位图数据结构既,通过用位来表示数字
					   t=[5,2,7]
				位图:	00100101   使用8位就可以表示集合中数据
					--- --- --- --- --- --- --- --- 
 				   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  				    --- --- --- --- --- --- --- --- 
					 0   0   1   0   0   1   0   1
	
				优点;节省空间(用很少空间就可以表示一个集合)、
	 				 节省时间(可以之间访问元素)
				缺点:集合必须无重复且,最多包含N个元素,且元素都小于N

				1.Byte 型数据占8位,正好可以放下

 ------------------------------------------------------ 
|jshell> Byte b = 7;                                   |
|b ==> 7                                               |
|                                                      |
|jshell> Integer.toBinaryString(b);                    |
|$33 ==> "111"                                         |
|                                                      |
|jshell> String t = Integer.toBinaryString(b);         |
|t ==> "111"                                           |
|                                                      |
|jshell> t.charAt(1);                                  |
|$35 ==> '1'                                           |
 ------------------------------------------------------
*/
/*
t=[5,2,7]
byte res = 0;
for(int i = 0;i<t.length;i++){
	res = res | (1<<(8-t[i]));
}
*/

import java.util.*;
public class ByteTest{
	private int res = 0;     //在执行运算时 byte、short、char进行运算时会自动转为int型
	//private List<Integer> t= null;
	public ByteTest(List<Integer> t)
	{
		show(calc(t));
	}
	public String calc(List<Integer> t){
		Iterator i = t.iterator();
		while(i.hasNext()){
			int j = (int)i.next();
			res = res|1<<(j);                //左移j位后和res进行“或运算”
		}
		return Integer.toBinaryString(res);  //转为2进制表示形式
	}
	public void show(String str){                //因String读取是从左向右的,而二进制表现形式是 从右到左,
		for(int i = 1;i<=str.length();i++){
			if(str.charAt(str.length()-i) == '1')		//所以要进行逆序遍历
				System.out.println(i-1);
		}
	}
	public static void main(String[] agrs)
	{
		List<Integer> t = new LinkedList<Integer>();
		t.add(5);
		t.add(2);
		t.add(7);		
		ByteTest b = new ByteTest(t);
	}
}

总结:

  1. java没有单独的对字节操作数据类型只能通过<< >>移位运算.
  2. 在执行运算时 byte、short、char进行运算时会自动转为int型.
  3. 集合结构无法使用int,char ,double等类型
  • int是Java的基本数据类型,长度为32位(4)字节;
  • Integer是Java提供的封装类,在java.lang.Integer包里面。
  • int默认值为0;而Integer默认值是null。
  • Integer是对象的引用,必须实例化后才能使用;int则不需要。
  • new一个Integer实际是生成一个指针指向此对象;而int则是直接存储数据值。
  • Integer有自动拆箱,自动装箱的功能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值