1.数组声明方式
String[] datas = {"a"};
String[] datas = new String[4];
String[] datas = new String[] {"a"};
2.排序
Integer[] dataArray = {2, 1, 4, 3};
// 升序
Arrays.sort(dataArray);
Arrays.sort(dataArray, (a, b) -> a - b);
// 降序
Arrays.sort(dataArray, (a, b) -> b - a);
3.数组复制
(1) Integer[] datas = {1, 2, 3};
Integer[] datas3 = Arrays.copyOf(datas, datas.length + 1);
注意:Arrays.copyOf()不能使用自动包装机制,Integer类型的值不能放入int数组里面,否则会报ArrayStoreException
(2) int[] datas2 = new int[3];
System.arraycopy(datas, 0, datas2, 0, datas.length);
注意:datas2必须申明长度,否则会报ArrayIndexOutOfBoundsException,即不能使用int[] datas2 = {};
System.arrcopy()不能使用自动包装机制,Integer类型的值不能放入int数组里面,会报ArrayStoreException
(3) for (int i = 0; i < datas.length; i++) {
Arrays.fill(datas2, i, i + 1, datas[i]);
}
Arrays.fill()能够使用自动包装机制,Integer类型的值能放入int数组里面
4.数组克隆
一维数组:深克隆(重新分配空间,将值复制过去)
二维数组:浅克隆(复制引用
Integer[] datas = {1, 2, 3};
Integer[] newArray = datas.clone();
int[][] d = {{1, 2}, {2, 3}};
int[][] c = d.clone();
System.out.println(d[1] == c[1]); // 结果为true,说明二维数组为浅克隆
实现二维数组深度克隆:对每一维数组调用clone方法
int[][] a={{3,1,4,2,5},{4,2}};
int[][] b=new int[a.length][];
for(int i=0;i<a.length;i++){
b[i]=a[i].clone();
}
System.out.println(a[0] == b[0]); // 结果为false
5.数组去重
int[] a = {1, 2, 1, 4};
Arrays.stream(a).distinct().forEach(b -> System.out.println(b));
6.数组比较
数组比较,只比较值,不比较内存的地址
Integer[] a = {1, 2};
Integer[] b = {1, 2};
boolean flag = Arrays.equals(a, b);
System.out.println(flag); // 结果为false
7.查找元素
int[] a = {1, 2, 3};
int index = Arrays.binarySearch(a, 3); // 获取指定元素的下标,未找到返回-1
8.数组转list
Integer[] a = {1, 2, 3};
List<Integer> list = Arrays.asList(a);
List<Integer> list2 = new ArrayList<>();
Collections.addAll(list2, a);
9.对象克隆
对象克隆,必须实现Cloneable接口,否则会报CloneNotSupportedException, Object中的clone()是浅克隆
public class User implements Cloneable, Serializable {
private static final long serialVersionUID = -1790086394592923350L;
private Integer id;
private String name;
private Role role;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
/**
* @param id
* @param name
* @param role
*/
public User(Integer id, String name, Role role) {
super();
this.id = id;
this.name = name;
this.role = role;
}
/**
*
*/
public User() {
super();
}
/* (non-Javadoc)
* @see java.lang.Object#clone()
*/
@Override
protected Object clone() throws CloneNotSupportedException {
User user = (User) super.clone();
user.role = (Role) this.role.clone();
return user;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", role=" + role + "]";
}
public User selfClone() {
User user = null;
try {
// 将该对象序列化成流,因为写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(this);
// 将流序列化成对象
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
user = (User) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return user;
}
}
public class Role implements Cloneable, Serializable {
/**
*
*/
private static final long serialVersionUID = 8674923130140275591L;
private Integer id;
private String name;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/**
* @param id
* @param name
*/
public Role(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
/**
*
*/
public Role() {
super();
}
/* (non-Javadoc)
* @see java.lang.Object#clone()
*/
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Role role = new Role(1, "管理员");
User user = new User(1, "张三", role);
// 深度克隆,重写clone()
User newUser = (User) user.clone();
// 使用序列化的方式,实现克隆,适应于对象多重引用场景
User newUser = user.selfClone();