代碼:java
import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.Test; /** * 數組的基本使用 */ public class ArrayDemo { /** * 數組的建立方法 * ------------------------------------------------------------ * 1)定值數組 * 2)定長數組 */ @Test public void testName1() throws Exception { // ==============================定值數組================================= // // 定值數組1 int[] intArr = { 1, 2, 3 }; Object[] objArr = { "", "", "" }; // 定值數組2 int[] intArr2 = new int[] { 1, 2, 3 }; Object[] objArr2 = new Object[] { "", "", "" }; // ==============================定長數組================================ // // 定長數組1 int[] intArr3 = new int[3]; Object[] objArr3 = new Object[3]; // 定長數組2 // |- 一維數組 int[] intArr4 = (int[]) Array.newInstance(int.class, 3); Object[] objArr4 = (Object[]) Array.newInstance(Object.class, 3); // |- 二維數組 int[] intArr5 = (int[]) Array.newInstance(int.class, 3, 3); Object[] objArr5 = (Object[]) Array.newInstance(Object.class, 3, 3); // |- 三維數組 int[] intArr6 = (int[]) Array.newInstance(int.class, 3, 3, 3); Object[] objArr6 = (Object[]) Array.newInstance(Object.class, 3, 3, 3); // ...可見建立到N維.... } /** * 定值數組做爲參數使用 * ------------------------------------------------------------ * 相似'{ 1, 2, 3 }、{ "", "", "" }'這樣的數組,是不能直接做爲參數使用的。 * 必須先賦值給某個聲明爲數組的變量,再使用那個變量做爲參數(賦值給變量的過程,應該是作了某種封裝)。 * 或、使用new字段建立一個直接賦值的數組對象做爲參數。 */ @Test public void testName2() throws Exception { // 定值數組1 int[] intArr = { 1, 2, 3 }; Object[] objArr = { "", "", "" }; // 做爲參數時使用 List<Object> list = new ArrayList<Object>(); // 用法1:ok list.add(intArr); list.add(objArr); // 用法2:編輯不能經過 /* list.add({ 1, 2, 3 }); list.add({ "", "", "" }); */ // 用法3:ok list.add(new int[] { 1, 2, 3 }); list.add(new Object[] { "", "", "" }); // 用法4:編輯不能經過 // test2( { 1, 2, 3 }); } public static void test2(Object[] objs) { System.out.println("參數個數:" + objs.length); System.out.println(Arrays.toString(objs)); } /** * Array的基本用法 * ---------------------------------------------------------------- * 使用Array的何種get方法取數據,取決於建立數組時聲明的元素類型。 * 若是建立數組時,聲明元素類型爲Object,就算存入一個int類型的值,也不能使用getInt方法獲取數組中的數據。 * 只有建立數組時聲明的元素類型是int,才能夠使用getInt方法獲取數組中的數據。 */ @Test public void testName3() throws Exception { Object[] objArr = (Object[]) Array.newInstance(Object.class, 3); // 設值 Array.set(objArr, 0, 1); // 插入一個int型數據 Array.set(objArr, 1, 1F); // 插入一個Float數據 Array.set(objArr, 2, ""); // 插入一個String數據 // 取值 // |- 錯誤的取法 // 會報錯:java.lang.IllegalArgumentException: Argument is not an array // int obj1 = Array.getInt(objArr, 0); // |- 正確的用法 Object obj2 = Array.get(objArr, 0); int num = (int) obj2; System.out.println(num); // 1 } /** * 關於數組傳參的另外一個問題 * -------------------------------------------------------------------------- * 1){ "1", "2", "3" }這樣的值,只能賦值給用數組形式聲明的變量。直接賦值給Object是編譯不能經過的。 * 2)Object obj和Object[] objArr雖然引用對象是同一個,可是由於聲明的類型不一致,做爲參數傳遞時,有很大差別。 * Object obj會做爲一個參數進行傳遞。 * Object[] objArr會做爲多個參數進行傳遞。 */ @Test public void testName4() throws Exception { // 定值數組1 Object[] objArr = { "1", "2", "3" }; test4(objArr); /* * 參數個數:3 * [1, 2, 3] */ // Object obj = { "1", "2", "3" }; // 編譯不經過 Object obj = objArr; test4(obj); /* * 參數個數:1 * [[Ljava.lang.Object;@e03bb5] */ } public static void test4(Object... objs) { System.out.println("參數個數:" + objs.length); System.out.println(Arrays.toString(objs)); } }