Java之旅_高级教程_实例_数组
摘自:http://www.runoob.com/java/java-examples.html
1.数组排序及元素查找
以下实例演示了如何使用sort()方法对Java数组进行排序,及如何使用 binarySearch() 方法来查找数组中的元素, 这边我们定义了 printArray() 方法来打印数组:
package javaLearn; import java.util.Arrays; public class MainClass{ public static void main(String[] args){ int array[] = {2,5,-2,6,-3,8,0,-7,-9,4}; Arrays.sort(array); printArray("数组排序结果为:",array); int index = Arrays.binarySearch(array, 2); System.out.println("元素2所在位置:"+index); } private static void printArray(String message,int array[]){ System.out.println(message+"[:length:"+array.length+"]"); for(int i=0;i<array.length;i++){ System.out.print(array[i]+" "); } System.out.println(); } }
2.数组添加元素
使用sort()方法排序,insertElement()方法向数组插入元素
package javaLearn; import java.util.Arrays; public class MainClass{ public static void main(String[] args){ int array[] ={2,5,-2,6,-3,8,0,-7,-9,4}; Arrays.sort(array); printArray("数组排序:",array); int index = Arrays.binarySearch(array, 1); System.out.println("元素1所在位置(负数为不存在):"+index); int newindex = -index-1; array = insertElement(array,1,newindex); printArray("数组添加元素1:",array); } private static void printArray(String message,int array[]){ System.out.println(message+"[:length:"+array.length+"]"); for(int i=0;i<array.length;i++){ System.out.print(array[i]+" "); } System.out.println(); } private static int[] insertElement(int array[],int element,int index){ int length = array.length; int destArray[] = new int[length+1]; System.arraycopy(array, 0, destArray, 0, index); destArray[index] = element; System.arraycopy(array, index, destArray, index+1, length-index); return destArray; } }
3.获取数组长度
public class MainClass{ public static void main(String[] args){ String[][] data = new String[2][5]; System.out.println(data.length); System.out.println(data[0].length); } }
4.数组反转
import java.util.ArrayList; import java.util.Collections; public class MainClass{ public static void main(String[] args){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("A"); arrayList.add("B"); arrayList.add("C"); System.out.println("排序前:"+arrayList); Collections.reverse(arrayList); System.out.println("排序后:"+arrayList); } }
5.数组输出
import java.util.ArrayList; import java.util.Collections; public class MainClass{ public static void main(String[] args){ String[] greeting = new String[3]; greeting[0] = "aaa"; greeting[1] = "bbb"; greeting[2] = "ccc"; for(int i=0;i<greeting.length;i++){ System.out.println(greeting[i]); } ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("A"); arrayList.add("B"); arrayList.add("C"); for(int i=0;i<arrayList.size();i++){ System.out.println(arrayList.get(i)); } } }
6.数组获取最大和最小值
import java.util.*; public class MainClass{ public static void main(String[] args){ Integer[] array = {8,2,7,1,4,9,5}; List<Integer> aList = Arrays.asList(array);//数组转换为list int min = Collections.min(aList); int max = Collections.max(aList); System.out.println("最小值为:"+min); System.out.println("最大值为:"+max); } }
7.数组合并
import java.util.*; public class MainClass{ public static void main(String[] args){ String[] a = {"A","B","C"}; String[] b = {"D","E","F"}; List<String> list = new ArrayList<String>(Arrays.asList(a)); //将数组a转换成list list.addAll(Arrays.asList(b)); //将数组b转换成list并添加到list中 Object[] c = list.toArray(); System.out.println(Arrays.toString(c)); } }
8.数组填充
import java.util.*; public class MainClass{ public static void main(String[] args){ String[] a = new String[6]; Arrays.fill(a, "aa"); for(int i=0;i<a.length;i++){ System.out.println(a[i]); } System.out.println("================="); Arrays.fill(a,3,6,"bb"); for(int i=0;i<a.length;i++){ System.out.println(a[i]); } } }
9.数组扩容
public class MainClass{ public static void main(String[] args){ 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 str : extended){ System.out.println(str); } } }
10.查找数组中重复元素
public class MainClass{ public static void main(String[] args){ int[] array = {1,2,3,4,5,2,5,6,7}; for(int i=0;i<array.length-1;i++){ for(int j=i+1;j<array.length;j++){ if(array[j]==array[i]){ System.out.println("重复元素:"+array[i]); } } } } }
11.删除数组元素
import java.util.*; public class MainClass{ public static void main(String[] args){ ArrayList<String> al = new ArrayList<String>(); al.clear(); al.add(0,"第一个元素"); al.add(1,"第二个元素"); al.add(2,"第三个元素"); al.remove(0); al.remove("第三个元素"); System.out.println(al); } }
12.数组差集
import java.util.*; public class MainClass{ public static void main(String[] args){ ArrayList<String> al = new ArrayList<String>(); ArrayList<String> al2 = new ArrayList<String>(); al.add(0,"第一个元素"); al.add(1,"第二个元素"); al.add(2,"hello"); al2.add(0,"第二个元素"); al2.add(1,"the second"); al2.add(2,"the third"); al2.removeAll(al); System.out.println("al2与al数组差集为:"+al2); } }
13.数组交集
import java.util.*; public class MainClass{ public static void main(String[] args){ ArrayList<String> al = new ArrayList<String>(); ArrayList<String> al2 = new ArrayList<String>(); al.add(0,"第一个元素"); al.add(1,"第二个元素"); al.add(2,"hello"); al2.add(0,"第二个元素"); al2.add(1,"the second"); al2.add(2,"the third"); al2.retainAll(al); System.out.println("al2与al数组交集为:"+al2); } }
14.在数组中查找指定元素
import java.util.*; public class MainClass{ public static void main(String[] args){ ArrayList<String> al = new ArrayList<String>(); ArrayList<String> al2 = new ArrayList<String>(); al.add(0,"第一个元素"); al.add(1,"第二个元素"); al.add(2,"hello"); al2.add(0,"第二个元素"); al2.add(1,"the second"); al2.add(2,"the third"); System.out.println("al2包含al?:"+al2.contains(al)); System.out.println("al2包含the second?"+al2.contains("the second")); } }
15.判断数组是否相等
import java.util.*; public class MainClass{ public static void main(String[] args){ int ary1[] = {1,2,3,4}; int ary2[] = {1,2,3,4,5}; int ary3[] = {1,2,3,4}; System.out.println("ary1与ary2相等?:"+Arrays.equals(ary1, ary2)); System.out.println("ary1与ary3相等?:"+Arrays.equals(ary1, ary3)); } }
16.数组并集
import java.util.*; public class MainClass{ public static void main(String[] args){ String[] ary1 = {"1","2","3","4"}; String[] ary2 = {"1","2","3"}; String[] ary3 = union(ary1,ary2); System.out.print("并集的结果:"); for(String str:ary3){ System.out.print(str+" "); } } public static String[] union(String[] ary1,String[] ary2){ Set<String> set = new HashSet<String>();//set 无序,不重复 for(String str:ary1){ set.add(str); } for(String str:ary2){ set.add(str); } String[] ary3 = {}; return set.toArray(ary3); } }