举个栗子:
// 创建可以保存任何数据类型的ArrayList
ArrayList<Object> a = new ArrayList();
a.add("1");
a.add(0);
a.add(new BigDecimal("2"));
a.add(4.00);
// 循环取值
for (Object obj : a) {
? ? System.out.println(obj);
}
// 根据类型取值
for (Object obj : a) {
? ? // 字符串类型
? ? if (obj instanceof String) {
? ? ? ? String b = (String) obj;
? ? ? ? System.out.println(b.getClass());
? ? }
? ? // 整型
? ? else if (obj instanceof Integer) {
? ? ? ? Integer b = (Integer) obj;
? ? ? ? System.out.println(b.getClass());
? ? }
? ? // BigDecimal
? ? else if (obj instanceof BigDecimal) {
? ? ? ? BigDecimal b = (BigDecimal) obj;
? ? ? ? System.out.println(b.getClass());
? ? }
? ? // Double
? ? else if (obj instanceof Double) {
? ? ? ? Double b = (Double) obj;
? ? ? ? System.out.println(b.getClass());
? ? } else {
? ? ? ? String b = "未检测到数据类型";
? ? ? ? System.out.println(b.getClass());
? ? }
}
使用比较少,特此记录下
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。