//二分查找,查不到返回 -(插入点+1)
int index = Collections.binarySearch(nums,30);
System.out.println(index);
//查到返回索引
index = Collections.binarySearch(nums,40);
System.out.println(index);
/*
Map:HashMap/TreeMap是典型的实现类。key-value,key不允许重复,是用Set来存放的。
*/
public class Demo04 {
public static void main(String[] args) {
//<String,String>分别是key,value的类型
Map<String,String> map = new HashMap<>();
//统计字符串中每个字符出现多少次。
String str = "This is a test.Today is a nice day.";
str = "HelloHi"; //str缩短有利于观察。
Map<Character,Integer> m = new HashMap<>();
//遍历每个字符
for (int i=0;i<str.length();i++){
//根据索引,取字符串中每个字符
char ch =str.charAt(i);
System.out.println("第"+i+"个字符为:"+ch);
//判断字符在Map中是否存在
if (m.get(ch) != null) {
//如果存在,根据字符取出次数,次数+1后,再放进去
int count = m.get(ch);
m.put(ch,count+1);
System.out.println(ch+"在map中存在,次数增加1.");
}else {
//如果不存在,将(字符,1)加入到Map中
m.put(ch,1); //T,1
System.out.println(ch+"不存在,加入到map中,次数设置为1.");
}
}
System.out.println("统计字符个数:"+m);
}
}
package day05;
import java.awt.geom.Arc2D;
/*
基本类型 包装类
int、 Integer
byte Byte
short Short
long Long
float Float
double Double
boolean Boolean
char Character
1、提供了一些实用的方法
2、集合中不能存放基本类型的数据,只能放引用类型,比如要存放数字,只能用Integer,不能用int
*/
public class Demo01 {
public static void main(String[] args) {
Integer num = 10;
int a = 10;
//valueof 把字符串转成Integer类型,radix表示字符串中的数字是几进制。
System.out.println(Integer.valueOf("1001",10));
System.out.println(Integer.valueOf("1001",8));
Float f = 10.5f;// 直接赋对应类型的值
Float f1 = new Float(10.1);//调用构造方法,不推荐使用
System.out.println(Float.valueOf("123")); //字符转换float
System.out.println(f1.longValue());//转换long类型
//拆箱、装箱
Boolean b = true;//把基本类型的值,直接赋值给对象b,自动装箱。
boolean b1 = b; //把包装类型的值,赋值给基本类型,自动拆箱。