代码如下:
Object objs[] = { 45, "king", 56.45, "rose", 45.4f, true, 'A', false,"Jack", 56, 0.0, 0.012f, 'B', true, 'C' };
String[] sa = new String[3];
int[] ia = new int[2];
boolean[] ba = new boolean[3];
char[] ca = new char[3];
float[] fa = new float[2];
double[] da = new double[2];
把objs数组里面的各类型值,按类型分别传到下面数组中~
怎么才能让他们自动分类呢??
作者: 790324255a 时间: 2014-7-11 14:10
按我下面的方式可以分类存储
public class Test2 {
public static void main(String[] args) {
Object objs[] = { 45, "king", 56.45, "rose", 45.4f, true, 'A', false,
"Jack", 56, 0.0, 0.012f, 'B', true, 'C' };
String[] sa = new String[3];
int[] ia = new int[2];
boolean[] ba = new boolean[3];
char[] ca = new char[3];
float[] fa = new float[2];
double[] da = new double[2];
int s = 0, i = 0, b = 0, c = 0, f = 0, d = 0;
for (Object o : objs) {
if (o instanceof String) {
sa[s++] = (String) o;
} else if (o instanceof Integer) {
ia[i++] = (int) o;
} else if (o instanceof Boolean) {
ba[b++] = (boolean) o;
} else if (o instanceof Character) {
ca[c++] = (char) o;
} else if (o instanceof Float) {
fa[f++] = (float) o;
} else if (o instanceof Double) {
da[d++] = (double) o;
}
}
}
}作者: 刘一博 时间: 2014-7-11 14:11 本帖最后由 刘一博 于 2014-7-11 14:13 编辑