准备数据:
============================================================
======================================
[table]
|输出结果:|
||
||
|Double:|
|Doubles[0] = null; Doubles[1] = null; Doubles[2] = null; Doubles[3] = null; Doubles[4] = null; |
||
|Float:|
|Floats[0] = null; Floats[1] = null; Floats[2] = null; Floats[3] = null; Floats[4] = null; |
||
|Long:|
|Longs[0] = null; Longs[1] = null; Longs[2] = null; Longs[3] = null; Longs[4] = null; |
||
|char:|
|chars[0] = ; chars[1] = ; chars[2] = ; chars[3] = ; chars[4] = ; |
||
|int:|
|ints[0] = 0; ints[1] = 0; ints[2] = 0; ints[3] = 0; ints[4] = 0; |
||
|Short:|
|Shorts[0] = null; Shorts[1] = null; Shorts[2] = null; Shorts[3] = null; Shorts[4] = null; |
||
|Boolean:|
|Booleans[0] = null; Booleans[1] = null; Booleans[2] = null; Booleans[3] = null; Booleans[4] = null; |
||
|long:|
|longs[0] = 0; longs[1] = 0; longs[2] = 0; longs[3] = 0; longs[4] = 0; |
||
|double:|
|doubles[0] = 0.0; doubles[1] = 0.0; doubles[2] = 0.0; doubles[3] = 0.0; doubles[4] = 0.0; |
||
|float:|
|floats[0] = 0.0; floats[1] = 0.0; floats[2] = 0.0; floats[3] = 0.0; floats[4] = 0.0; |
||
|Character:|
|Characters[0] = null; Characters[1] = null; Characters[2] = null; Characters[3] = null; Characters[4] = null; |
||
|short:|
|shorts[0] = 0; shorts[1] = 0; shorts[2] = 0; shorts[3] = 0; shorts[4] = 0; |
||
|Byte:|
|Bytes[0] = null; Bytes[1] = null; Bytes[2] = null; Bytes[3] = null; Bytes[4] = null; |
||
|byte:|
|bytes[0] = 0; bytes[1] = 0; bytes[2] = 0; bytes[3] = 0; bytes[4] = 0; |
||
|String:|
|Strings[0] = null; Strings[1] = null; Strings[2] = null; Strings[3] = null; Strings[4] = null; |
||
|boolean:|
|booleans[0] = false; booleans[1] = false; booleans[2] = false; booleans[3] = false; booleans[4] = false; |
||
|Integer:|
|Integers[0] = null; Integers[1] = null; Integers[2] = null; Integers[3] = null; Integers[4] = null; |
||
|Object:|
|Objects[0] = null; Objects[1] = null; Objects[2] = null; Objects[3] = null; Objects[4] = null; |
||
||
||
|如果有好的方法请回个帖!!|
||
[/table]
package test1;
import java.util.HashMap;
import java.util.Map;
public class MyArray {
//8种基本数据类型
static byte bytes[] = new byte[5];
static char chars[] = new char[5];
static int ints[] = new int[5];
static short shorts[] = new short[5];
static long longs[] = new long[5];
static float floats[] = new float[5];
static double doubles[] = new double[5];
static boolean booleans[] = new boolean[5];
//10种封装类
static Byte[] Bytes = new Byte[5];
static Character[] Characters = new Character[5];
static Short[] Shorts = new Short[5];
static Integer[] Integers = new Integer[5];
static Long[] Longs = new Long[5];
static Float[] Floats = new Float[5];
static Double[] Doubles = new Double[5];
static Boolean[] Booleans = new Boolean[5];
static String[] Strings = new String[5];
static Object[] Objects = new Object[5];
static Map<String,Object> arrayMap = new HashMap<String,Object>();
public static Map<String, Object> initMap(){
//8种基本数据类型
arrayMap.put("byte", bytes);
arrayMap.put("char", chars);
arrayMap.put("int", ints);
arrayMap.put("short", shorts);
arrayMap.put("long", longs);
arrayMap.put("float", floats);
arrayMap.put("double", doubles);
arrayMap.put("boolean", booleans);
//10种封装类
arrayMap.put("Byte", Bytes);
arrayMap.put("Character", Characters);
arrayMap.put("Short", Shorts);
arrayMap.put("Integer", Integers);
arrayMap.put("Long", Longs);
arrayMap.put("Float", Floats);
arrayMap.put("Double", Doubles);
arrayMap.put("Boolean", Booleans);
arrayMap.put("String", Strings);
arrayMap.put("Object", Objects);
return arrayMap;
}
}
============================================================
package test1;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ArrayDefaultValue {
public static void main(String[] args) throws Exception {
Map<String, Object> arrayMap = MyArray.initMap();
ArrayDefaultValue adf = new ArrayDefaultValue();
adf.setArguments(arrayMap);
}
//该方法只能对封装类有效!基本数据类型的数组无法使用
public static void printArrayDefaultValues(String key, Object[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(key + "s[" + i + "] = " + arr[i] + "; ");
}
System.out.println();
}
数据类型匹配
private void setArguments(Map<String, Object> arrayMap) throws Exception {
Set<String> mapkey = arrayMap.keySet();
Iterator<String> it = mapkey.iterator();
while (it.hasNext()) {
String key = it.next();
Object value = arrayMap.get(key);
// System.out.println(key + "------"+value);
System.out.println("\n" + key + ":");
if (key.intern() == "byte") {
byte[] arr = (byte[]) value;
for (int i = 0; i < arr.length; i++) {
System.out.print(key + "s[" + i + "] = " + arr[i] + "; ");
}
System.out.println();
//Cannot cast from byte[] to Object[],无法调用下面这个方法
// printArrayDefaultValues(key,arr);
} else if (key.intern() == "Byte") {
Byte[] arr = (Byte[]) value;
printArrayDefaultValues(key, arr);
} else if (key.intern() == "char") {
char[] arr = (char[]) value;
for (int i = 0; i < arr.length; i++) {
System.out.print(key + "s[" + i + "] = " + arr[i] + "; ");
}
System.out.println();
} else if (key.intern() == "Character") {
Character[] arr = (Character[]) value;
printArrayDefaultValues(key, arr);
} else if (key.intern() == "int") {
int[] arr = (int[]) value;
for (int i = 0; i < arr.length; i++) {
System.out.print(key + "s[" + i + "] = " + arr[i] + "; ");
}
System.out.println();
} else if (key.intern() == "Integer") {
Integer[] arr = (Integer[]) value;
printArrayDefaultValues(key, arr);
} else if (key.intern() == "short") {
short[] arr = (short[]) value;
for (int i = 0; i < arr.length; i++) {
System.out.print(key + "s[" + i + "] = " + arr[i] + "; ");
}
System.out.println();
} else if (key.intern() == "Short") {
Short[] arr = (Short[]) value;
printArrayDefaultValues(key, arr);
} else if (key.intern() == "long") {
long[] arr = (long[]) value;
for (int i = 0; i < arr.length; i++) {
System.out.print(key + "s[" + i + "] = " + arr[i] + "; ");
}
System.out.println();
} else if (key.intern() == "Long") {
Long[] arr = (Long[]) value;
printArrayDefaultValues(key, arr);
} else if (key.intern() == "float") {
float[] arr = (float[]) value;
for (int i = 0; i < arr.length; i++) {
System.out.print(key + "s[" + i + "] = " + arr[i] + "; ");
}
System.out.println();
} else if (key.intern() == "Float") {
Float[] arr = (Float[]) value;
printArrayDefaultValues(key, arr);
} else if (key.intern() == "double") {
double[] arr = (double[]) value;
for (int i = 0; i < arr.length; i++) {
System.out.print(key + "s[" + i + "] = " + arr[i] + "; ");
}
System.out.println();
} else if (key.intern() == "Double") {
Double[] arr = (Double[]) value;
printArrayDefaultValues(key, arr);
} else if (key.intern() == "boolean") {
boolean[] arr = (boolean[]) value;
for (int i = 0; i < arr.length; i++) {
System.out.print(key + "s[" + i + "] = " + arr[i] + "; ");
}
System.out.println();
} else if (key.intern() == "Boolean") {
Boolean[] arr = (Boolean[]) value;
printArrayDefaultValues(key, arr);
} else if (key.intern() == "String") {
String[] arr = (String[]) value;
printArrayDefaultValues(key, arr);
} else if (key.intern() == "Object") {
Object[] arr = (Object[]) value;
printArrayDefaultValues(key, arr);
} else {
throw new RuntimeException("unkown arguments type");
}
}
}
}
======================================
[table]
|输出结果:|
||
||
|Double:|
|Doubles[0] = null; Doubles[1] = null; Doubles[2] = null; Doubles[3] = null; Doubles[4] = null; |
||
|Float:|
|Floats[0] = null; Floats[1] = null; Floats[2] = null; Floats[3] = null; Floats[4] = null; |
||
|Long:|
|Longs[0] = null; Longs[1] = null; Longs[2] = null; Longs[3] = null; Longs[4] = null; |
||
|char:|
|chars[0] = ; chars[1] = ; chars[2] = ; chars[3] = ; chars[4] = ; |
||
|int:|
|ints[0] = 0; ints[1] = 0; ints[2] = 0; ints[3] = 0; ints[4] = 0; |
||
|Short:|
|Shorts[0] = null; Shorts[1] = null; Shorts[2] = null; Shorts[3] = null; Shorts[4] = null; |
||
|Boolean:|
|Booleans[0] = null; Booleans[1] = null; Booleans[2] = null; Booleans[3] = null; Booleans[4] = null; |
||
|long:|
|longs[0] = 0; longs[1] = 0; longs[2] = 0; longs[3] = 0; longs[4] = 0; |
||
|double:|
|doubles[0] = 0.0; doubles[1] = 0.0; doubles[2] = 0.0; doubles[3] = 0.0; doubles[4] = 0.0; |
||
|float:|
|floats[0] = 0.0; floats[1] = 0.0; floats[2] = 0.0; floats[3] = 0.0; floats[4] = 0.0; |
||
|Character:|
|Characters[0] = null; Characters[1] = null; Characters[2] = null; Characters[3] = null; Characters[4] = null; |
||
|short:|
|shorts[0] = 0; shorts[1] = 0; shorts[2] = 0; shorts[3] = 0; shorts[4] = 0; |
||
|Byte:|
|Bytes[0] = null; Bytes[1] = null; Bytes[2] = null; Bytes[3] = null; Bytes[4] = null; |
||
|byte:|
|bytes[0] = 0; bytes[1] = 0; bytes[2] = 0; bytes[3] = 0; bytes[4] = 0; |
||
|String:|
|Strings[0] = null; Strings[1] = null; Strings[2] = null; Strings[3] = null; Strings[4] = null; |
||
|boolean:|
|booleans[0] = false; booleans[1] = false; booleans[2] = false; booleans[3] = false; booleans[4] = false; |
||
|Integer:|
|Integers[0] = null; Integers[1] = null; Integers[2] = null; Integers[3] = null; Integers[4] = null; |
||
|Object:|
|Objects[0] = null; Objects[1] = null; Objects[2] = null; Objects[3] = null; Objects[4] = null; |
||
||
||
|如果有好的方法请回个帖!!|
||
[/table]