Arrays类
java.util.Arrays类
用于操作数组工具类,里面定义了常见操作数组的静态方法。
equals方法:
比较两个非同一数组是否相等,而数组本身的equals判断另一个数组是否它本身。
声明:public static boolean equals(type[]a,type[]a2)
参数的类型可以是原生数据类型和引用类型的任意一种类型。
返回:如果两个相等,则返回true,否则返回false
package day3;
import java.util.Arrays;
public class EqualsDemo {
public static void main(String[] args) {
int [] a={1,2,3};
int [] b={1,2,3};
System.out.println(Arrays.equals(a,b));//比较俩个数组每个位置上的元素是否相等
System.out.println(a==b);
}
}
sort -排序:
自定义对象排序
1.自定义类实现Comparable接口
2.重写compareTo方法
package day3;
import java.util.Arrays;
public class SortDemo {
public static void main(String[] args) {
int [] a={1,2,3,5,4,62};
Arrays.sort(a);//对整个数组 升序排列 快速排序法
Arrays.sort(a,0,5); //对数组指定位置进行排序 包含开始 不包含结束位置
System.out.println(Arrays.toString(a));
}
}
package day3;
import java.util.Arrays;
public class SortDemo2 {
public static void main(String[] args) {
Student s1=new Student("weitao1",121);
Student s2=new Student("weitao2",122);
Student s3=new Student("weitao3",123);
Student s4=new Student("weitao4",124);
Student[] stuArrays={s1,s4,s2,s3};
Arrays.sort(stuArrays);
System.out.println("a".compareTo("b")); // b-a(98-97)
System.out.println(Arrays.toString(stuArrays));
}
}
package day3;
public class Student implements Comparable<Student> { //排序接口
private String name;
private int num;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", num=" + num +
'}';
}
public Student(String name, int num) {
this.name = name;
this.num = num;
}
/*
* Arrays类中的一个方法Sort 调用compareTo接口实现数组中元素(名字,学号)的排序
* 返回值 结果有3种<0 =0 >0
*
*
* */
@Override
public int compareTo(Student o) {
// return this.num-o.num; //学号排序 <0 =0 >0
return this.name.compareTo(o.name); //名字排序
}
}
二分查找:
声明:
public static int binarySearch(type[] a, type key)
public static int binarySearch(long[] a,int fromIndex,int toIndex,long key)
描述:
使用二分搜索算法搜索指定的type型数组,以获得指定的值。
参数:
a - 要搜索的数组。
key -
要搜索的值。
fromIndex - 要排序的第一个元素的索引(包括)。
toIndex - 要排序的最后一个元素的索引(不包括)。
type
-byte、double、float、object、long、int、short、char
如果key在数组中,则返回搜索值的索引;否则返回-1或者”-“(插入点)
package day3;
import java.util.Arrays;
public class SerchDemo {
public static void main(String[] args) {
/*
二分搜索法/折半查找
前提是必须有序的
*/
int [] a={3,2,6,4,8,1,7};
/*
如果没有找到 返回一个负数
如果有 返回key的索引值
*/
Arrays.sort(a);
System.out.println(Arrays.toString(a));
// int index =Arrays.binarySearch(a,3);
//在某个区间查找 包含开始 不包含结束
int index =Arrays.binarySearch(a,0,4,2);
System.out.println(index); //排序后为参照物查找
}
}
package day3;
import java.util.Arrays;
public class SerchDemo1 {
public static void main(String[] args) {
int []s={1,2,3,4,5,6,7};
SerchDemo1.serch(s,4);
}
/*
二分查找
*/
public static void serch(int []s,int key) {
int low = 0; //低位
int high = s.length - 1;//高位
while (low <= high) {
int mid = (low + high) >>> 1;//相当于除2
int midVal = s[mid]; //中间值
if (midVal < key)
low = mid + 1; //中间值小 去右边查找
else if (midVal > key)
high = mid - 1; //中间值大去左边查找
else
System.out.println(mid);
return;
}
System.out.println(-1);
}
}