Java [unchecked]未经检查的大小写警告 [英] Java [unchecked] unchecked case warning

查看:161
本文介绍了Java [unchecked]未经检查的大小写警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在四处寻找Google搜索,但仍然找不到避免这种警告的方法.

Ok, I've been looking around and done alot of google searching, but I still can't find a way to avoid this warning.

Integer result = chooser.showOpenDialog(null);
if (result.equals(0))
{
    String tempHolder = chooser.getSelectedFile().getPath();
    filenameLoad = new File(tempHolder);
    filenameSave = filenameLoad;
    FileInputStream fis = null;
    ObjectInputStream in = null;
    try
    {
        fis = new FileInputStream(filenameLoad);
        in = new ObjectInputStream(fis);;
    }
    catch(IOException ex)
    {
        ex.printStackTrace();
    }

    try
    {
        loadFile = (ArrayList<Dot>)in.readObject();
    }
    catch(IOException ex)
    {
        System.out.println("Cast fail");
    }
    catch(ClassNotFoundException ex)
    {
        System.out.println("Cast fail");
    }
    catch (ClassCastException ex)
    {
        System.out.println("Cast fail");
    }

    try
    {
        in.close();
    }
    catch(Exception ex)
    {
        System.out.println("failed to close in");
    }
    save.setEnabled(true);
      gpanel.setDotList(loadFile);
  }

它在行loadFile =(ArrayList)in.readObject();中给我警告.我已经添加了渔获量,所以我不确定为什么它仍然说未捕获.有什么帮助吗?谢谢吗?

It gives me the warning at the line loadFile = (ArrayList)in.readObject(); I've added in the catchs so i'm not sure why it still says its uncatched. Any help? thanks?

推荐答案

它不是未捕获"的,而是未选中的". JVM无法在运行时(即完成转换时)确定ArrayList是否确实包含Dot元素.

It is not "uncatched", but "unchecked". The JVM cannot tell at runtime, i.e. when the cast is done, whether the ArrayList really contains Dot elements.

每当您从原始类型转换为泛型类型时,都会发生此警告.如果确定投放正确,则可以通过注释取消警告

This warning occurs whenever you cast from a raw type to a generic type. If you are sure the cast is ok, you can suppress the warning with annotation

@SuppressWarnings("unchecked")

为此,最好使用小的单独方法封装演员表.

For this, it is good to encapsulate the cast in a small, separate method.

这篇关于Java [unchecked]未经检查的大小写警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆