Java 数组详解 - 用法、遍历、排序、实用API
博客来源:http://baike.xsoftlab.net/view/241.htmlJava 数组详解 - 用法、遍历、排序、实用API数组java排序遍历实用api用法概要:数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标。组成数组的各个变量称为数组的分量,也称
·
博客来源:http://baike.xsoftlab.net/view/241.html
Java 数组详解 - 用法、遍历、排序、实用API
概要:
数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标。
组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量。
数组是在程序设计中,为了处理方便, 把具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。
| |目录
初始化数组
动态初始化
int[] array = new int[3]; // 两种方式都可以 // int array[] = new int[3]; array[0] = 1; array[1] = 2; array[2] = 3; System.out.println(array[0]);
静态初始化
int[] array = { 1, 2, 3, 4 };
int[] array1 = new int[] { 1, 2, 3, 4 };
System.out.println(array[2]);
System.out.println(array1[2]);
默认初始化
int[] array = new int[4]; System.out.println(array[2]);
获取数组的长度
int[] array = new int[10];
int length = array.length;
System.out.println("数组array的长度是:" + length);
二维数组
二维数组初始化
同一维数组一样,共有4总不同形式的定义方法:
int[][] array1 = new int[10][10];
int array2[][] = new int[10][10];
int array3[][] = { { 1, 1, 1 }, { 2, 2, 2 } };
int array4[][] = new int[][] { { 1, 1, 1 }, { 2, 2, 2 } };
不定长二维数组
int[][] array = new int[3][]; array[0] = new int[1]; array[1] = new int[2]; array[2] = new int[3];
获取二维数组的长度
int length1 = array.length; int length2 = array[0].length; // 获取二维数组的第一维长度(3) System.out.println(length1); // 获取二维数组的第一维的第一个数组长度(1) System.out.println(length2);
数组遍历
传统方式遍历
int[] array = new int[] { 1, 2, 3 };
for (int i = 0; i < array.length; i++) {
System.out.println("array[" + i + "] = " + array[i]);
}
增强for循环方式遍历
int[] array = new int[] { 1, 2, 3 };
for (int i : array) {
System.out.println(i);
}
数组排序
常用的排序方法有冒泡排序、快速排序、选择排序、插入排序、希尔(Shell)排序、堆排序。可参考Java 数组排序
数组的复制、插入和排序
实用API
输出数组
int[] array = { 1, 2, 3 };
System.out.println(Arrays.toString(array));
数组转List
String[] array = { "a", "b", "c", "d", "e" };
List<String> list = new ArrayList<String>(Arrays.asList(array));
System.out.println(list);
数组转Set
String[] array = { "a", "b", "c", "d", "e" };
Set<String> set = new HashSet<String>(Arrays.asList(array));
System.out.println(set);
List转数组
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
String[] array = new String[list.size()];
list.toArray(array);
for (String s : array)
System.out.println(s);
数组中是否包含某个值
String[] array = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(array).contains("a");
System.out.println(b);
数组合并
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 6, 7, 8, 9, 10 };
int[] array = org.apache.commons.lang.ArrayUtils.addAll(array1, array2);
System.out.println(Arrays.toString(array));
String数组转字符串(使用指定字符拼接)
String[] array = { "a", "b", "c" };
String str = org.apache.commons.lang.StringUtils.join(array, ", ");
System.out.println(str);
数组逆序
int[] array = { 1, 2, 3, 4, 5 };
org.apache.commons.lang.ArrayUtils.reverse(array);
System.out.println(Arrays.toString(array));
数组元素移除
int[] array = { 1, 2, 3, 4, 5 };
int[] removed = org.apache.commons.lang.ArrayUtils.removeElement(array, 3);
System.out.println(Arrays.toString(removed));
数组异常
1. 数组下标越界异常,java.lang.ArrayIndexOutOfBoundsException
在java中,数组下标从0开始,我们定义了一个长度为3的数组,想要从中取出第四位,便会抛出此异常。
int[] array = new int[] { 1, 2, 3 };
System.out.println(array[3]);
更多干货等你来拿 http://www.itit123.cn/
更多推荐



所有评论(0)