Arrays的简单使用
程序员文章站
2024-03-06 22:31:02
...
- copyOfRange 数组复制
- toString() 转换为字符串
- sort 排序
- binarySearch 搜索
- equals 判断是否相同
- fill 填充
int[] b = Arrays.copyOfRange(a, 0, 3);
int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
String content = Arrays.toString(a);
public class HelloWorld {
public static void main(String[] args) {
int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
System.out.println("排序之前 :");
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println("排序之后:");
System.out.println(Arrays.toString(a));
}
}
public class HelloWorld {
public static void main(String[] args) {
int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
Arrays.sort(a);
System.out.println(Arrays.toString(a));
//使用binarySearch之前,必须先使用sort进行排序
System.out.println("数字 62出现的位置:"+Arrays.binarySearch(a, 62));
}
}
public class HelloWorld {
public static void main(String[] args) {
int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
int b[] = new int[] { 18, 62, 68, 82, 65, 8 };
System.out.println(Arrays.equals(a, b));
}
}
Arrays.fill(a, 5); //5个5