一 Arrays 工具类
1.排序 short
String[] str={"nba","abc","cba","aaaa","z","qq"}; Arrays.sort(str); System.out.println(Arrays.toString(str));
[aaaa, abc, cba, nba, qq, z]当数组为字母是比较Acill码 ,为数字时直接比较大小;
2. 查找 binarySearch
int[] arr= {1,2,3,4,5}; int a = Arrays.binarySearch(arr, 6); System.out.println(a);
-6
返回值:
如果它包含在数组中,则返回搜索键的索引;
否则返回 (-(插入点) - 1)。插入点 被定义为将键插入数组的那一点:
即第一个大于此键的元素索引,如果数组中的所有元素都小于指定的键,则为 a.length。
注意,这保证了当且仅当此键被找到时,返回的值将 >= 0。
二 StringBuffer 和 StringBuilder
1.两者的区别
StringBuffer
JDK1.0 出现 线程安全 耗费系统资源
StringBuilder
JDK1.5 出来 线程不安全 节省系统资源 速度较快,建议使用
StringBuffer & StringBuilder 可变的字符序列 (操作对象时 是对对象本身修改)
操作的方法一般是没有返回值.
String
线程不安全 不可变的字符序列(不能修改字符串本身)
2.两者的方法基本一致,以下为一些简单的操作方法
1.拼接
public static void fun1() { // 创建一个 Stringbuff 对象 StringBuffer sb = new StringBuffer(); // 容量 System.out.println(sb.capacity()); //长度 System.out.println(sb.length()); //从后追加 sb.append("dahai").append("chishi").insert(5,"嘿嘿嘿"); System.out.println(sb); // buffer 是个对象 打印其toString 方法 //Stringbuff 转化成字符串类型 String string = sb.toString(); System.out.println(string); }
16 // 16表示字节 0 dahai嘿嘿嘿chishi dahai嘿嘿嘿chishi2.插入
public static void fun2() { //插入 StringBuffer sBuffer = new StringBuffer("dahai"); sBuffer.insert(2, "da").append("nb"); // 2表示插入位置的首字符下标 System.out.println(sBuffer); //修改 sBuffer.setCharAt(1, 'x'); System.out.println(sBuffer); // 将下标为1的字符替换为x }
dadahainb dxdahainb
3.删除
public static void fun3() { StringBuffer sb = new StringBuffer(); sb.append("dahai"); //删除 留头不留尾[0,2) //传入的结束坐标大于字符串的长度 相当于清空字符串 sb.delete(0, 2); System.out.println(sb);//hai // 注意 直接传入的是要删除字符的索引值 sb.deleteCharAt(0); System.out.println(sb);//ai // 获取StringBuff 中的字符 char c = sb.charAt(1); System.out.println(c);//i // 字符数组 转化成StringBuff 对象 char [] arr = {'a','b','c'}; String string = String.valueOf(arr); StringBuffer sub = new StringBuffer(string); System.out.println(sub);//abc }
4.反转
public static void fun4() { // 键盘输入一个字符串 反转 System.out.println("请输入一个字符串"); Scanner scanner = new Scanner(System.in); String string = scanner.nextLine(); StringBuffer sb = new StringBuffer(string); sb.reverse(); System.out.println(sb); }
5.替换
public static void 替换() { //替换 StringBuffer sBuffer = new StringBuffer(); sBuffer.append("ddddhai"); sBuffer.replace(0, 4, "lixiao"); System.out.println(sBuffer); }
看一个例题
需求 把int[] array = new int[]{1,2,3,4}; 输出上面的 [1, 2, 3, 4] 要求:使用两种方法(String 和 StringBuffer)
public static void fun5() { int[] array = new int[]{1,2,3,4}; String str = "["; for (int i = 0; i < array.length; i++) { if (i==array.length-1) { // 最后一位拼 ] str = str + array[i] +"]"; }else { str+=array[i]+","; } } StringBuffer sb = new StringBuffer() ; sb.append('['); for (int i = 0; i < array.length; i++) { if (i!=array.length-1) { sb.append(array[i]).append(','); }else { sb.append(array[i]).append(']'); } } System.out.println(sb); System.out.println(str); }
当 String StringBuffer StringBuilder 的类做参数时 string和基本数据类型一样是值得传递, StringBuffer StringBuilder 则是地址的传递.
private static void fun1(String str) { str = "haha"; } private static void fun2(StringBuffer sb) { sb.append("haha"); } private static void fun3(StringBuilder sd) { sd.append("haha"); } public static void main(String[] args) { // 3个操作字符串的类当做参数时的效果 // 字符串 当参数的时候 和基本数据类型一样 是值传递 String string = "wanglong"; fun1(string); System.out.println(string);// wanglong StringBuffer sb = new StringBuffer("wanglong"); fun2(sb); System.out.println(sb);//wanglonghaha StringBuilder sd = new StringBuilder("wanglong"); fun3(sd); System.out.println(sd);//wanglonghaha }
基本数据类型的包装类
byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Character将基本数据类型分装成一个类, 类中就可以有成员方法,静态方法.功能进行了扩展.
public static void fun() { //利用int类型的对象创建一个interger对象 Integer integer = new Integer(60); Integer n1 = new Integer(10); // 转化成数字 n1.intValue(); System.out.println(n1);//10 System.out.println(integer);//60 System.out.println(Integer.toBinaryString(integer));//111100 System.out.println(Integer.toHexString(integer));//3c System.out.println(Integer.toOctalString(integer));//74 //利用字符串来创建对象 Integer integer2 = new Integer("123"); System.out.println(integer2);//123 //把integer类型转换成int类型 int number1 =integer2.intValue(); System.out.println(number1);//123 //把字符串类型转换成int类型 int number2 = Integer.parseInt("123456"); System.out.println(number2);//123456 /* * 1.5版本以后,添加了自动装箱和自动拆箱功能 */ // 一般直接用数字来声明 // 相当于系统帮忙调用了 Integer.valueof(); Integer number3 = 10;//自动装箱 Integer integer2 = new Integer("10"); int sum = number3+5;//自动拆箱,其实是umber3.intvalue(); //系统自动装箱拆箱其实就是帮你调用转化方法 System.out.println(sum);//15 }
三 集合
1.分类
单列集合
双列集合
数组的弊端 就是集合的好处 1.长度一旦确定 不能改变 2.只能保存 相同数据类型的 元素
要注意的是 集合中只能存储引用数据类型
集合的用法
首先创建一个学生类 :
public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; }
//创建一个学生集合 保存5个学生 打印学生姓名
Collection collection = new ArrayList(); collection.add( new Student("Vld",1)); collection.add( new Student("Vtd",2)); collection.add( new Student("Vrd",3)); collection.add( new Student("Vnd",4)); collection.add( new Student("Vyd",5)); // 集合转数组 //向上转型 Object[] array = collection.toArray(); //向下转型 // 需要把数组中的每一个对象都转化从student 类型 //Student [] students = (Student[])array; 错误转法 for (int i = 0; i < array.length; i++) { //将数组每一个对象转化 Student s = (Student)array[i]; System.out.println(s.getName()); }
下面是集合的一些用法
public static void fun() { //多态的形式创建 Collection collection = new ArrayList(); boolean b1 = collection.add("a"); boolean b2 = collection.add("b"); boolean b3 = collection.add(false); //当直接向集合中存放基本数据类型时 //系统会帮你进行继续装箱 //设计思想 返回值适用下面的所有子接口 set集合是有可能添加失败的. boolean b4 = collection.add(10); System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(b4); // 打印元素个数 System.out.println(collection.size()); // 判断是否包含某个元素 boolean rel1 =collection.contains(false); System.out.println(rel1); // 从集合中删除一个元素 boolean rel2 = collection.remove("ooo"); System.out.println(rel2); //判断集合 是否为空 boolean rel3 =collection.isEmpty(); System.out.println(rel3); // 清空数组 // collection.clear(); // 集合转数组 Object[] array = collection.toArray(); for (Object object : array) { System.out.println(object); } System.out.println(collection); }