1.字符串编码
字符串的字符在内存中使用Unicode编码,一个字符(不区分字母还是汉字)占两个字节。在进行显示的时候,会转成其他编码 ```java import java.lang.reflect.Array; import java.nio.charset.StandardCharsets; import java.util.Arrays; public class Hello { public static void main(String[] args) throws Exception{ String a = “嘟嘟666”;//idea 的编码为UTF_8,自然下面读取的时候就是UTF_8方式读取的 String b = new String(a.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);// UTF_8读取 / ISO_8859_1解析 System.out.println(b);//乱码 String b1 = new String(b.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);// ISO_8859_1读取 / UTF_8解析 System.out.println(b1);//乱码复原 new string 方式 String b2;// ISO_8859_1读取 / UTF_8解析
byte[]UTF_8_1= a.getBytes(StandardCharsets.UTF_8); String s = Arrays.toString(UTF_8_1); System.out.println(s+” “+UTF_8_1.length);//[-27, -104, -97, -27, -104, -97, 54, 54, 54] 9 //一般常用的中文字符utf-8,占三个字节 [-27, -104, -97,-27, -104, -97 byte[]ISO_8859_1_1= a.getBytes(StandardCharsets.ISO_8859_1); String s1 = Arrays.toString(ISO_8859_1_1); System.out.println(s1+” “+ISO_8859_1_1.length);//[63, 63, 54, 54, 54] 5 //ISO_8859_1方式编码,中文占一个字节, [63, 63,
byte[] A=b.getBytes(StandardCharsets.ISO_8859_1);
byte[] B=b1.getBytes(StandardCharsets.UTF_8);
System.out.println(Arrays.toString(A)+” / “+Arrays.toString(B));// //[-27, -104, -97, -27, -104, -97, 54, 54, 54] / [-27, -104, -97, -27, -104, -97, 54, 54, 54]相等
char[] chars1111 = a.toCharArray(); String s2 = Arrays.toString(chars1111); System.out.println(s2); //每个字符都占两个字节 }}
- hex编码解码
```java
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import java.nio.charset.StandardCharsets;
public class Hello {
public static void main(String[] args) throws Exception{
String a = "嘟嘟666";
byte[] a_bytes = a.getBytes(StandardCharsets.UTF_8);
String s=HexBin.encode(a_bytes);//E5989FE5989F363636
System.out.println(s);
byte[] s1= HexBin.decode(s);
String aaa=new String(s1);
System.out.println(aaa);
}
}
2.StringBuffer
- 为什么需要StringBuffer
- StringBuffer特性
a) StringBuffer的构造器
b) StringBuffer是一个final类, 不能被继承
c) StringBuffer的父类是AbstractStringBuilder
d) AbstractStringBuilder有属性char[] value, 存放字符串内容,存放在堆中 - StringBuffer与String的转换
StringBuffer stringBuffer = new StringBuffer(“xiaojianbang”);
stringBuffer = stringBuffer.append(“java”);
String s = stringBuffer.toString();
String ss = new String(stringBuffer); - StringBuffer的方法
append、toString、delete、replace、indexOf、insert、length、reverse String、StringBuffer、StringBuilder
a) String: 不可变字符序列、效率低、但是复用率高
b) StringBuffer: 可变字符序列、效率较高(增删)、线程安全
c) StringBuilder: 可变字符序列、效率最高、线程不安全 ```java import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin; import java.nio.charset.StandardCharsets; public class Hello { public static void main(String[] args) throws Exception{ StringBuffer aa = new StringBuffer(“dudu”); aa.append(“123”); System.out.println(aa.toString());//dudu123StringBuilder bb = new StringBuilder(“张家界军军军军”); bb.append(“555555555”); System.out.println(bb.toString());//张家界军军军军555555555} }}
<a name="ouUYI"></a>
#### 3.Arrays
1. Math相关方法的使用 查API<br />Math类包含用于执行基本数学运算的方法,如指数、对数、平方根和三角函数
1. Arrays里面包含了一系列静态方法,用于管理或操作数组(比如排序和搜索)<br />Arrays.toString 返回数组的字符串形式<br />Arrays.sort 自然排序和定制排序(数值排序、对象排序)<br />Arrays.copyOf 数组元素的复制<br />Arrays.asList 数组转List<br />Arrays.fill 数组填充<br />Arrays.binarySearch 二分查找(有序数组)
1. System<br />System.exit() 退出程序<br />System.arraycopy 复制数组<br />System.currentTimeMillens() 返回当前时间距离1970-1-1的毫秒数<br />System.gc() 运行垃圾回收机制
```java
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Comparator;
public class Hello {
public static void main(String[] args) throws Exception{
int[] aa=new int[]{3,55,99,1,2,3,4,5,6,7,8,9,10};
Arrays.sort(aa);
System.out.println( Arrays.toString(aa));//[1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 55, 99]从小到大
Integer[] bb=new Integer[]{3,55,99,1,2,3,4,5,6,7,8,9,10};//Integer包装类
Arrays.sort(bb, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;//o2-o1 与 o1-o2 导致的排序结果不一样 return 0;则按照原样返回
}
});
System.out.println( Arrays.toString(bb));
}}
对象排序 ```java import java.util.Arrays; import java.util.Comparator; public class Hello { public static void main(String[] args) throws Exception{ int[] aa=new int[]{3,55,99,1,2,3,4,5,6,7,8,9,10}; Arrays.sort(aa); System.out.println( Arrays.toString(aa));//[1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 55, 99]从小到大 Integer[] bb=new Integer[]{3,55,99,1,2,3,4,5,6,7,8,9,10};//包装类 Arrays.sort(bb, new Comparator
() { @Override
public int compare(Integer o1, Integer o2) {
return o2-o1;//o2-o1 与 o1-o2 导致的排序结果不一样 return 0;则按照原样返回
}
}); System.out.println( Arrays.toString(bb)); System.out.println(“==============上方数组排序,下方对象属性排序=================”); person person = new person(“叶秋”,30); person person1 = new person(“小叶”,20); person person2 = new person(“小秋”,10); person[] aaaa=new person[]{person,person1,person2}; Arrays.sort(aaaa, new Comparator
() { @Override
public int compare(person o1, person o2) {
return o2.age-o1.age;
}
}); System.out.println( Arrays.toString(aaaa)); //[person{name=’叶秋’, age=10}, person{name=’小叶’, age=20}, person{name=’小秋’, age=30}] //按照对象的属性排序 //应用场景:app签名时排列数据
}} class person{ public String name=null; public int age=0; public person(String name, int age) {
this.name = name;
this.age = age;
} @Override public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
} }
- 数组拷贝相关方法
```java
import java.util.Arrays;
import java.util.List;
public class Hello {
public static void main(String[] args) throws Exception {
int[] aa = new int[]{3, 55, 99, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};//数组是引用传递的
int[] cc =new int[50];//这里开辟的数组大小必须大于或等于被拷贝数字的长度大小;
Arrays.fill(cc,123); //数组填充
System.out.println("test/ "+Arrays.toString(cc));
int[] bb = Arrays.copyOf(aa, aa.length);
System.out.println(Arrays.toString(bb));
System.arraycopy(aa,2,cc,4,aa.length-2);
System.out.println(Arrays.toString(cc));
// System.arraycopy(待被拷贝数组,从第0个数组开始拷贝,目标数组,放在新数组的第0个位置,拷贝的长度);
List<Object> dd = Arrays.asList(cc);//这个函数是把数组转换成集合
//二分法查找
Arrays.sort(aa);
System.out.println(Arrays.toString(aa));
System.out.println(Arrays.binarySearch(aa, 99));//查找传入的参数,必须是有序的,计次,返回的是查找文本的下标,返回负数就是没有找到
}}
5.BigInteger
- Biglnteger适合保存比较大的整型
- BigDecimal适合保存精度更高的浮点型(小数)
常用方法
a) add 加
b) subtract 减
c) multiply 乘
d) divide 除import java.math.BigInteger;
public class Hello {
public static void main(String[] args) throws Exception {
BigInteger a1 = new BigInteger("11111111111111111111111111111111111111111111");
BigInteger a2 = new BigInteger("55555555555555555555555551111111111111111111111111111111111111111155555");
BigInteger add = a1.add(a2);
BigInteger multiply = a1.multiply(a2);
BigInteger divide = a1.divide(a2);
BigInteger subtract = a1.subtract(a2);
System.out.println(add);
System.out.println(multiply);
System.out.println(divide);
System.out.println(subtract);
}}
6.集合
为什么需要集合
a) 数组创建时长度必须指定,而且后续不能更改
b) 数组保存的必须为同一类型的元素
c) 数组增加/删除元素比较麻烦
d) 集合可以动态保存任意多个对象
e) 集合提供了一系列方便的方法操作对象: add、remove、set、get等集合框架体系
a) 集合主要是两组Collection和Map(单列集合,双列集合)
b) Collection接口有两个子接口List、Set,其实现子类都是单列集合
c) Map接口的实现子类是双列集合,存放的K-Vimport java.util.ArrayList;
import java.util.HashMap;
public class Hello {
public static void main(String[] args) throws Exception {
HashMap hashMap = new HashMap();
hashMap.put("x", "y");
Object x = hashMap.get("x");
System.out.println(x);//y 双列集合
ArrayList arrayList = new ArrayList();//单列集合
arrayList.add("cccc");
arrayList.add("vvvv");
System.out.println(arrayList);//[cccc, vvvv]
}}
7.Collection
add 添加单个元素,都是Object,基本数据类型加入的是包装类型
- remove 删除指定元素,List的子类有重载,可以按index删除
- contains 查找元素是否存在
- size 获取元素个数
- isEmpty 判断是否为空
- clear 清空
- addAll 添加多个元素
- containsAll 查找多个元素是否都存在
removeAll 删除多个元素
import java.util.ArrayList;
import java.util.Collection;
public class Hello {
public static void main(String[] args) throws Exception {
Collection Collection = new ArrayList(); //Collection提供方法,则会在接口里面取寻找其实现方法
Collection.add("11111111111");
Collection.add(333);
Collection.add(new int[]{111111111});
Collection.add(false);
Collection.add(null);
System.out.println(Collection); //ArrayList是有序的
Collection Collection1 = new ArrayList();
Collection1.add("88888");
Collection1.add(3335555);
Collection.add(Collection1);
System.out.println(Collection);//[11111111111, 333, [I@1540e19d, false, null, [88888, 3335555]]
}}
8.List
相较于collection,list更加有序,可以根据下标操作
- List集合中元素有序,且可重复
- List集合中的每个元素都有对应的索引,从0开始
- List可以加入各种元素,可以重复加入,包括null
List集合里增加了一些根据索引来操作集合元素的方法
a) void add(int index, Object ele) 在index位置插入ele元素
b) boolean addAll(int index, Collection eles) 从index位置开始将eles中的所有元素添加进来
c) Object get(int index) 获取指定index位置的元素
d) int indexOf(Object obj) 返回obj在集合中首次出现的位置
e) int lastIndexOf(object obj) 返回obj在当前集合中末次出现的位置
f) Object remove(int index) 移除指定index位置的元素,并返回此元素
g) Object set(int index, Object ele) 设置指定index位置的元素为ele,相当于是替换
h) List subList(int fromlndex, int tolndex) 返回从fromIndex到tolndex位置的子集合import java.util.ArrayList;
import java.util.List;
public class Hello {
public static void main(String[] args) throws Exception {
List Collection = new ArrayList(); //Collection提供方法,则会在接口里面取寻找其实现方法
Collection.add("11111111111");
Collection.add(333);
Collection.add(new int[]{111111111});
Collection.add(false);
Collection.add(null);
Collection.add(1,"在index位置插入ele元素");
List Collection1 = new ArrayList(); //Collection提供方法,则会在接口里面取寻找其实现方法
Collection1.add("Collection1插入1");
Collection1.add("Collection1插入2");
Collection.addAll(2,Collection1);
System.out.println(Collection.get(2));
System.out.println(Collection);
}}
9.list的遍历
a) Iterator对象称为迭代器,用于遍历Collection集合中的元素
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Hello {
public static void main(String[] args) throws Exception {
List Collection = new ArrayList(); //Collection提供方法,则会在接口里面取寻找其实现方法
Collection.add("11111111111");
Collection.add(333);
// Iterator对象称为迭代器,用于遍历Collection集合中的元素
//快捷键 itit
Iterator iterator = Collection.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
}}
b)增强for循环
其实就是迭代器遍历 快捷输入 iter/xxx.forimport java.util.ArrayList;
import java.util.List;
public class Hello {
public static void main(String[] args) throws Exception {
List Collection = new ArrayList(); //Collection提供方法,则会在接口里面取寻找其实现方法
Collection.add("11111111111");
Collection.add(333);
//增强for循环
for (Object o : Collection) {
System.out.println(o);
}
//第三种方式遍历
for (int i = 0; i < Collection.size(); i++) {//这种方法必须要有索引
System.out.println("第三种方式:"+Collection.get(i));
}
}}
10.ArrayList
ArrayList特点
a) ArrayList是由数组来实现数据存诸的
b) Vector与ArrayList基本相同,Vector线程安全, ArrayList执行效率高import java.util.ArrayList;
public class Hello {
public static void main(String[] args) throws Exception {
ArrayList arrayList = new ArrayList();
for (int i = 0; i < 10; i++) {
arrayList.add("1111");
arrayList.add(i );
System.out.println(arrayList);
}
}}
11.Vector
Vector也是用一个对象数组存放元素,protected Object[] elementData;
- Vector与ArrayList基本相同,Vector线程安全, ArrayList执行效率高
- Vector扩容机制与ArrayList相似,只不过是按2倍扩容
a) 使用无参构造器,创建ArrayList对象时,初始elementData容量为10,如需再次扩 容,则扩容elementData为2倍
b) 使用指定大小的构造器,初始elementData容量为指定大小,如需扩容,则直接扩容 elementData为2倍 Vector可以自己指定每次扩容个数;new Vector(10, 3);
import java.util.Vector;
public class Hello {
public static void main(String[] args) throws Exception {
Vector Vector = new Vector();
for (int i = 0; i < 10; i++) {
Vector.add(1); Vector.add("2");;
System.out.println(Vector);
}
}}
12.LinkedList
LinkedList特点
a) LinkedList底层是一个双向链表
b) LinkedList中有两个属性first和last分别指向首节点和尾节点
c) 每个节点(Node对象)里面又有prev、next、item三个属性,其中item中存放 元素,prev指向前一个节点,next指向后一个节点,实现双向链表
d) 简易双向链表演示- LinkedList与ArrayList比较
a) LinkedList的元素的添加和删除,不是通过数组完成的,相对来说效率较高
b) ArrayList对改查的效率较高,改查操作多的时候选择ArrayList
c) 一般程序都是查询操作比较多,ArrayList比较常用一点
d) LinkedList和ArrayList都是线程不安全的 - LinkedList源码分析
//原理图
public class Hello {
public static void main(String[] args) throws Exception {
node 叶秋 = new node("叶秋");
node 小叶 = new node("小叶");
node 小秋 = new node("小秋");
叶秋.next=小叶;
小叶.next=小秋;//正向链接
小秋.prev=小叶;
小叶.prev=叶秋;//反向链接
node fisrt =叶秋;//定义链表第一个
node last =小秋;//定义链表最后一个
node tmp =fisrt;//临时变量
while (tmp!=null){//正向遍历
System.out.println(tmp);
tmp =tmp.next;
}
System.out.println("=======================");
tmp =last;//临时变量
while (tmp!=null){//反向遍历
System.out.println(tmp);
tmp =tmp.prev;
}
System.out.println("=======================");
node java = new node("java");//指定位置添加元素
java.prev=叶秋;
java.next=小叶;
小叶.prev=java;
叶秋.next=java;
tmp =叶秋;//临时变量
while (tmp!=null){//正向遍历
System.out.println(tmp);
tmp =tmp.next;
}
}}
class node{
public node prev;
public node next;
public Object item;
public node(Object item) {
this.item = item;
}
@Override
public String toString() {
return "node{" +
"item=" + item +
'}';
}
}
- 使用方法
import java.util.LinkedList;
public class Hello {
public static void main(String[] args) throws Exception {
LinkedList linkedList = new LinkedList();
linkedList.add("叶秋");
linkedList.add(5);
linkedList.add(null);
for (Object o : linkedList) {
System.out.println(o);
}
}}
13.Set
- 无序(添加和取出的顺序不一致),没有索引
- 取出的顺序虽然不是添加的顺序,但是取出的顺序固定
- 不允许重复元素
- Set接口也是Collection的子接口,常用方法和Collection接口一样
Set接口的遍历方式
a) 迭代器遍历
b) 增强for遍历
c) 不能使用索引的方式来获取,因此无法用普通for循环遍历import java.util.HashSet;
import java.util.Iterator;
public class Hello {
public static void main(String[] args) throws Exception {
HashSet hashSet = new HashSet();
hashSet.add(null);
hashSet.add(null);//前方已添加,后续添加不了
hashSet.add("叶秋");
hashSet.add("小叶");
hashSet.add("小秋");
hashSet.remove("叶秋");//移除
Iterator iterator = hashSet.iterator();//HashSet只能用这两个方法遍历
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
System.out.println("=================");
for (Object o : hashSet) {
System.out.println(o);
}
}}
/*
set.add("叶秋"); //添加成功
set.add("叶秋"); //添加失败
set.add(new Dog("tom")); //对象hashcode不一致 添加成功
set.add(new Dog("tom")); //对象hashcode不一致 添加成功
set.add(new String("叶秋")); //内容一致 添加失败
set.add(new String("叶秋")); //内容一致 添加失败
*/
14.hashset练习
通过重写equals和hashCode方法,实现当两个对象name和age值相同的时候,不能加入HashSet里面中。
import java.util.HashSet;
import java.util.Objects;
public class Hello {
public static void main(String[] args) throws Exception {
a a = new a("叶秋",30);
a b = new a("叶秋",30);
a c = new a("小叶",30);
a d = new a("小叶",30);
a e = new a("小秋",30);
a f = new a("小秋",30);
HashSet hashSet = new HashSet();
hashSet.add(a);
hashSet.add(b);
hashSet.add(c);
hashSet.add(d);
hashSet.add(e);
hashSet.add(f);
System.out.println(hashSet);
}}
class a{
public String name=null;
public int age=0;
public a(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
a a = (a) o;
return age == a.age && name.equals(a.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "a{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
15.LinkedHashSet
LinkedHashSet不允许添重复元素
- LinkedHashSet是HashSet的子类
LinkedHashSet底层是一个LinkedHashMap(HashMap的子类) - LinkedHashSet存放元素的方式跟HashSet相同,只不过它在这基础上又维护了一个双向链表,这个双向链表使得元素遍历的顺序和添加的顺序一致
总结:输出顺序与输入顺序一致,不允许添加重复元素import java.util.*;
public class Hello {
public static void main(String[] args) throws Exception {
LinkedHashSet linkedHashSet = new LinkedHashSet();
linkedHashSet.add("123");
linkedHashSet.add("12");
linkedHashSet.add("1");
System.out.println(linkedHashSet);//[123, 12, 1]
}}