API—Arrays类
目录
1 概述
Arrays 类是一个数组工具类,在java.util包中,需要使用Arrays类的方法时,导入Arrays类:
import java.util.Arrays;
其中包含了数组操作的很多方法。这个 Arrays 类里均为 static 修饰的方法(static 修饰的方法可以直接通过类名调用),可以直接通过 Arrays.xxx(xxx) 的形式调用方法。
2 示例
2.1Arrays.equals()
- Arrays.equals()
比较不同两数组内容是否相同
- equals()
比较两数组在内存中地址是否相等,即判断另一数组是否是其本身
//两方法源码
//Arrays.equals()
public static boolean equals(int[] a, int[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++)
if (a[i] != a2[i])
return false;
return true;
//equals()
public boolean equals(Object obj) {
return (this == obj);
}
int [] a={1,2,3,4,5};
int [] b={5,4,3,2,1};
int [] c={1,2,3,4,5};
System.out.println(a==c);//比较数组a,c在内存中的地址
System.out.println(Arrays.equals(a,c));//判断两数组内容是否一致
System.out.println(a.equals(c));//判断两者是否是同一对象
/*
输出:
false
true
false
*/
2.2Arrays.sort()
- public static void sort(int[] a)
对整个数组排序(升序)
- public static void sort(int[] a, int fromIndex, int toIndex)
对数组指定区间排序(升序)
a:要排序的数组
fromIndex:排序开始索引(包括)
toIndex:排序结束索引(不包括,toIndex-1)
int [] a={5,4,3,2,1};
/*
全部排序
*/
Arrays.sort(a);
System.out.println(Arrays.toString(a));
/*
输出数组时,默认使用toString()输出地址
*/
System.out.println(a.toString());//[I@1b6d3586
System.out.println(a);//a.toString [I@1b6d3586
/*
[1, 2, 3, 4, 5]
[I@1b6d3586
[I@1b6d3586
*/
int [] a={5,4,3,2,1};
/*
局部排序
*/
Arrays.sort(a,0,3);//第0索引~第3-1索引
//将数组转为字符串输出
System.out.println(Arrays.toString(a));
/*
输出:
[3, 4, 5, 2, 1]
*/
String [] str={"d","c","b","a","e"};
//Arrays.sort(str);//排序
Arrays.sort(str,0,3);//0~3-1 --> 0~2 将指定区间的元素排序
System.out.println(Arrays.toString(str));
/*
输出:
[b, c, d, a, e]
*/
2.2.1 自定义对象排序
自定义类,实现Comparable接口,重写其compareTo()方法
public class Student implements Comparable<Student> {//实现Comparale接口
private String name;
private int num;
public Student(int num,String name) {
this.num = num;
this.name = name;
}
//用来作比较,提供一个自定义排序规则的方法
@Override//compareTo()方法会在sort()的底层调用
public int compareTo(Student o) {
//return this.num-o.num;//大于0 等于0 小于0 三种返回值
return this.name.compareTo(o.name);
}
@Override
public String toString() {
return "Student{" +
"num=" + num +
", name='" + name +
'\'' +
'}';
}
}
public static void main(String[] args) {
Student [] std=new Student[5];
Student s1=new Student(101,"ABC");
Student s2=new Student(102,"AB");
Student s3=new Student(103,"A");
Student s4=new Student(104,"ABCDE");
Student s5=new Student(105,"ABCD");
std[0]=s5;
std[1]=s4;
std[2]=s3;
std[3]=s2;
std[4]=s1;
//对引用数据类型-->数组进行排序
Arrays.sort(std);
System.out.println(Arrays.toString(std));
}
/*
输出:
[Student{num=103, name='A'},
Student{num=102, name='AB'},
Student{num=101, name='ABC'},
Student{num=105, name='ABCD'},
Student{num=104, name='ABCDE'}]
*/
2.3Arrays.BinarySearch()
二分查找:查询指定指在数组中的位置
前提: 数组是有序的
int [] a={6,5,2,3,25,33,3,64,1,8,0,10};
Arrays.sort(a);//先排序,再查找
System.out.println(Arrays.binarySearch(a,7));//-8
System.out.println(Arrays.binarySearch(a,4,8,3));//从a[4]~a[8]找3
System.out.println(binarySearch(a,3));
/*
输出:
-8
4
3
*/
/*方法源码:
public static int binarySearch(int[] a, int key) {
int low = 0;//开始索引
int high = a.length- 1;//结束索引
while (low <= high) {//
int mid = (low + high) >>> 1;
//找中间位置 右移一位==除2 0100 4 >>> 0010 2
int midVal = a[mid];//得到的中间值
if (midVal < key)//若中间值<寻找值
low = mid + 1;
else if (midVal > key)//中间值>寻找值
high = mid - 1;
else
return mid; // key found
}
return -1; // key not found.
}
*/
2.4Arrays.copyOf()
- Arrays.copyOf()
返回一个指定长度的新数组,并将原数组中的内容复制到新数组中
int [] a={1,4,2,6,3,9,6,5,0};
/*
public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
*/
//返回一个指定长度的新数组,并将原数组中的内容复制到新数组中
System.out.println(Arrays.toString(Arrays.copyOf(a,15)));//复制数组到新数组中
System.out.println(Arrays.toString(Arrays.copyOf(a,5)));//复制数组到新数组中
/*
输出
[1, 4, 2, 6, 3, 9, 6, 5, 0, 0, 0, 0, 0, 0, 0]
[1, 4, 2, 6, 3]
*/
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/15647.html