赞
踩
java.util.Arrays 类能方便地操作数组,它提供的所有方法都是静态的。
使用时导包:import java.util.Arrays
具有以下功能(部分):
输出数组:通过toString方法不需要循环遍历直接输出数组
对数组排序:通过 sort 方法将数组升序排列。
复制数组:通过copyOf方法将一个数组的值赋值给另一个数组
1.通常我们输出数组会使用普通的for循环来循环输出数组
int array[] = {1,2,3,4,5};
for(int i=0;i<array.length;i++) {
System.out.print(array[i]);
}
输出结果为
12345
2.或者我们可以用增强for循环 for each循环来循环输出
int array[] = {1,2,3,4,5};
for(int i:array) {
System.out.print(array[i-1]);
}
输出结果为
12345
这里要注意for each 循环下标从1开始 所以要将i-1来实现数组从下标0开始遍历
3.利用Array类中的toString方法 返回数组的字符串形式
int array[] = {1,2,3,4,5};
System.out.println(Arrays.toString(array));
输出结果为
[1, 2, 3, 4, 5]
当直接输出array或者array.toString()时,输出的会是数组的首地址
int array[] = {1,2,3,4,5};
System.out.println(array);
System.out.println(array.toString());
输出结果为
[I@7637f22
[I@7637f22
全部代码
package tostring; import java.util.Arrays; public class ToString { public static void main(String[] args) { int array[] = {1,2,3,4,5}; System.out.print("for循环输出数组: "); for(int i=0;i<array.length;i++) { System.out.print(array[i]); } System.out.println(); //for each 循环下标从1开始 System.out.print("for each循环输出数组: "); for(int i:array) { System.out.print(array[i-1]); } System.out.println(); System.out.print("toString方法输出数组: "); System.out.println(Arrays.toString(array)); System.out.println(array); System.out.println(array.toString()); } }
输出结果为
for循环输出数组: 12345
for each循环输出数组: 12345
toString方法输出数组: [1, 2, 3, 4, 5]
[I@7637f22
[I@7637f22
sort()常用来默认升序
int array[] = {1,4,5,3,6,2};
System.out.println("排序前");
System.out.println(Arrays.toString(array));
Arrays.sort(array);
System.out.println("排序后");
System.out.println(Arrays.toString(array));
输出结果为
排序前
[1, 4, 5, 3, 6, 2]
排序后
[1, 2, 3, 4, 5, 6]
sort()还可以用来局部排序
public static void main(String[] args) {
int array[] = {1,4,5,3,6,2};
System.out.println("排序前");
System.out.println(Arrays.toString(array));
Arrays.sort(array,1,4);
System.out.println("排序后");
System.out.println(Arrays.toString(array));
}
输出结果为
排序前
[1, 4, 5, 3, 6, 2]
排序后
[1, 3, 4, 5, 6, 2]
这里的(array,1,4)即意为在array数组中array[from_index]~array[to_index-1]
sort()降序排列
sort方法是默认升序,实现降序需要传入一个实现Comparator接口的类
Comparator中不支持基本类型所以要把int改为Integer
Integer c[] = {1,4,5,3,6,2};
System.out.println("排序前");
System.out.println(Arrays.toString(c));
Arrays.sort(c, new Comparator<Integer>()
{
public int compare(Integer c1, Integer c2) {
return c2-c1;
}
});
System.out.println("排序后");
System.out.println(Arrays.toString(c));
}
输出结果为
排序前
[1, 4, 5, 3, 6, 2]
排序后
[6, 5, 4, 3, 2, 1]
把一个数组的值传给另一个数组
package tostring; import java.util.Arrays; public class CopyOf { public static void main(String[] args) { int a[] = {1,2,3,4,5,6}; System.out.println("a数组为:"+Arrays.toString(a)); //输出a数组 int b[] = Arrays.copyOf(a,4); // 把a数组的前四位数赋给b数组 int c[] = Arrays.copyOf(a,8); System.out.println("b数组为:"+Arrays.toString(b)); System.out.println("c数组为:"+Arrays.toString(c)); int d[] = Arrays.copyOfRange(a, 2,4); // 把a数组的a[2]~a[4-1]赋给c数组 a[from_index]~a[to_index-1] System.out.println("d数组为:"+Arrays.toString(d)); } }
输出结果为
a数组为:[1, 2, 3, 4, 5, 6]
b数组为:[1, 2, 3, 4]
c数组为:[1, 2, 3, 4, 5, 6, 0, 0]
d数组为:[3, 4]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。