怎么处理这个异常?java.lang.ClassCastException: java.util.Date cannot be cast to java
importjava.util.ArrayList;importjava.util.Date;importjava.util.List;publicclassTest01...
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Test01 {
public static void main(String[] args) {
List list=new ArrayList();
list.add("aaa");
list.add(new Date());
list.add(1234);//自动装箱
list.add(new Dog());
System.out.println(list.size());
list.remove("aaa");
System.out.println(list.size());
List list2=new ArrayList();
list2.add("bbb");
list2.add("ccc");
list.add(list2);
//顺序有关
String str=(String)list.get(0);
System.out.println(str);
}
}
class Dog{
}
//编译是通过的 展开
import java.util.Date;
import java.util.List;
public class Test01 {
public static void main(String[] args) {
List list=new ArrayList();
list.add("aaa");
list.add(new Date());
list.add(1234);//自动装箱
list.add(new Dog());
System.out.println(list.size());
list.remove("aaa");
System.out.println(list.size());
List list2=new ArrayList();
list2.add("bbb");
list2.add("ccc");
list.add(list2);
//顺序有关
String str=(String)list.get(0);
System.out.println(str);
}
}
class Dog{
}
//编译是通过的 展开
展开全部
类型转换出错,
Data类型不能转成String
如果非要转的话用String.valueOf(data);
类型转换错误,不能把Date型强转为String型
java代码1
public int getAllUserNum(){
Session session = HiberSF.getSession();
try {
session.getTransaction().begin();
//解决办法直接调用:HashCode();
//count(*)也可以理解为返回一个Object的对象,然后直接
//调用 hashCode();
Integer lo = (Integer)session.createQuery("select count(*) from User").uniqueResult().hashCode();
session.getTransaction().commit();
return sum;
} catch (RuntimeException e) {
session.getTransaction().rollback();
throw e;
// TODO: handle exception
} finally {
session.close();
}
}
java代码1
public int getAllUserNum(){
Session session = HiberSF.getSession();
try {
session.getTransaction().begin();
Long lo = (Long)session.createQuery("select count(*) from User").uniqueResult();
//解决办法把long型拆分字符串进行转换
Integer sum = new Integer(String.valueOf(lo));
session.getTransaction().commit();
return sum;
} catch (RuntimeException e) {
session.getTransaction().rollback();
throw e;
// TODO: handle exception
} finally {
session.close();
}
}
Data类型不能转成String
如果非要转的话用String.valueOf(data);
类型转换错误,不能把Date型强转为String型
java代码1
public int getAllUserNum(){
Session session = HiberSF.getSession();
try {
session.getTransaction().begin();
//解决办法直接调用:HashCode();
//count(*)也可以理解为返回一个Object的对象,然后直接
//调用 hashCode();
Integer lo = (Integer)session.createQuery("select count(*) from User").uniqueResult().hashCode();
session.getTransaction().commit();
return sum;
} catch (RuntimeException e) {
session.getTransaction().rollback();
throw e;
// TODO: handle exception
} finally {
session.close();
}
}
java代码1
public int getAllUserNum(){
Session session = HiberSF.getSession();
try {
session.getTransaction().begin();
Long lo = (Long)session.createQuery("select count(*) from User").uniqueResult();
//解决办法把long型拆分字符串进行转换
Integer sum = new Integer(String.valueOf(lo));
session.getTransaction().commit();
return sum;
} catch (RuntimeException e) {
session.getTransaction().rollback();
throw e;
// TODO: handle exception
} finally {
session.close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询