package myjava; /** * 数组 * @author Administrator * */ import java.util.*; public class Myarray { public static void main(String args[]) { // 数组排序 int array[] = { 1, 9, 4, 2, 8, 6, 7 }; Arrays.sort(array); // 数组输出 for (int item : array) { System.out.println(item); } // 数组转字符串 String str = Arrays.toString(array); System.out.println(str); // 获取数组长度 System.out.println("数组长度为:" + array.length); int[][] newArray = new int[2][5]; System.out.println("第二维数组长度: " + newArray[0].length); // 数组反转 ArrayList arrayList = new ArrayList(); arrayList.add("a"); arrayList.add("b"); arrayList.add("c"); Collections.reverse(arrayList); System.out.println("反转后排序: " + arrayList); // 数组最大值最小值 Integer[] numners = { 8, 3, 6, 1, 4, 7 }; int min = (int) Collections.min(Arrays.asList(numners)); int max = (int) Collections.max(Arrays.asList(numners)); System.out.println("最小值为: " + min); System.out.println("最大值为: " + max); // 数组合并 String a[] = { "a", "b", "c" }; String b[] = { "d", "e" }; List list = new ArrayList(Arrays.asList(a)); list.addAll(Arrays.asList(b)); Object[] c = list.toArray(); System.out.println("数组C为:" + Arrays.toString(c)); // 数组填充 int array2[] = new int[6]; Arrays.fill(array2, 100); System.out.println("填充后的数组为:" + Arrays.toString(array2)); // 数组扩容 String[] names = new String[] { "A", "B", "C" }; String[] extended = new String[5]; extended[3] = "D"; extended[4] = "E"; System.arraycopy(names, 0, extended, 0, names.length); for (String row : extended) { System.out.println(row); } // 删除数组元素 ArrayList arrayList2 = new ArrayList(); arrayList2.clear(); arrayList2.add(0, "第 0 个元素"); arrayList2.add(1, "第 1 个元素"); arrayList2.add(2, "第 2 个元素"); arrayList2.remove(1); System.out.println("数组删除元素后:"+arrayList2); } }
原文:http://my.oschina.net/kenblog/blog/424512