基本数据类型和封装类数组初始化值的研究
准备数据:
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");
}
}
}
}
======================================
|输出结果:| || || |Double:| |Doubles = null; Doubles = null; Doubles = null; Doubles = null; Doubles = null; | || |Float:| |Floats = null; Floats = null; Floats = null; Floats = null; Floats = null; | || |Long:| |Longs = null; Longs = null; Longs = null; Longs = null; Longs = null; | || |char:| |chars = ; chars = ; chars = ; chars = ; chars = ; | || |int:| |ints = 0; ints = 0; ints = 0; ints = 0; ints = 0; | || |Short:| |Shorts = null; Shorts = null; Shorts = null; Shorts = null; Shorts = null; | || |Boolean:| |Booleans = null; Booleans = null; Booleans = null; Booleans = null; Booleans = null; | || |long:| |longs = 0; longs = 0; longs = 0; longs = 0; longs = 0; | || |double:| |doubles = 0.0; doubles = 0.0; doubles = 0.0; doubles = 0.0; doubles = 0.0; | || |float:| |floats = 0.0; floats = 0.0; floats = 0.0; floats = 0.0; floats = 0.0; | || |Character:| |Characters = null; Characters = null; Characters = null; Characters = null; Characters = null; | || |short:| |shorts = 0; shorts = 0; shorts = 0; shorts = 0; shorts = 0; | || |Byte:| |Bytes = null; Bytes = null; Bytes = null; Bytes = null; Bytes = null; | || |byte:| |bytes = 0; bytes = 0; bytes = 0; bytes = 0; bytes = 0; | || |String:| |Strings = null; Strings = null; Strings = null; Strings = null; Strings = null; | || |boolean:| |booleans = false; booleans = false; booleans = false; booleans = false; booleans = false; | || |Integer:| |Integers = null; Integers = null; Integers = null; Integers = null; Integers = null; | || |Object:| |Objects = null; Objects = null; Objects = null; Objects = null; Objects = null; | || || || |如果有好的方法请回个帖!!| ||