一、什么是Arrays
人话:能对数组操作的工具包
(1)练习
将随机字符串升序并倒序输出
public class Text1 {
public static void main(String[] args) {
String s = "123456789";
char[] c = s.toCharArray();
Arrays.sort(c);
for(int i = c.length -1 ; i>=0 ;i--)
{
System.out.println(c[i]);
}
}
}
(2)equals 对比方法
public class XiaoZhengClassMate3 {
public static void main(String[] args) {
String[] a1 = {"1111","2222","3333"};
String[] a2 = {"1111","2222","3333"};
boolean e = Arrays.equals(a1,a2);
System.out.println(e);
}
}
(3)tostring 以字符串输出
public class XiaoZhengClassMate3 {
public static void main(String[] args) {
int[] a = new int[]{1,2,3,4,5,6,7,8,9};
System.out.println(Arrays.toString(a));
}
}
(4)sort() 排序
public class XiaoZhengClassMate3 {
public static void main(String[] args) {
int[] a = new int[]{5,6,7,8,9,1,2,3,4};
Arrays.sort(a);
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
(5)fill 填充
public class XiaoZhengClassMate3 {
public static void main(String[] args) {
int[] a = new int[]{1,2,3,4,5,6,7,8,9,0};
Arrays.fill(a,1);
System.out.println(Arrays.toString(a));
}
}
(6)binarySearch 查找元素值相同的下标
public class XiaoZhengClassMate3 {
public static void main(String[] args) {
int[] a = new int[]{1,2,9,3,4,5,6,7,8,0};
int index =Arrays.binarySearch(a,1);
System.out.println(index);
}
}
(7)copyOf 截取 元素下标 传入 新的 数组
public class XiaoZhengClassMate3 {
public static void main(String[] args) {
int[] a = new int[]{1,2,9,3,4,5,6,7,8,0};
int[] a1 = new int[3];
a1=Arrays.copyOf(a,2);
System.out.println(Arrays.toString(a1));
}
}
(8)copyOfRange 截取 元素下标 传入 新的 数组
public class XiaoZhengClassMate3 {
public static void main(String[] args) {
int[] a = new int[]{1,2,9,3,4,5,6,7,8,0};
int[] a1 = new int[3];
a1=Arrays.copyOfRange(a,1,2);
System.out.println(Arrays.toString(a1));
}
}