javac抱怨未经检查的类型转换。

huangapple go评论62阅读模式
英文:

javac complaining about unchecked cast

问题

我是一名JavaScript开发者,正在进行Java练习,需要帮助。

当我尝试使用javac进行编译时,出现以下错误:
"uses unchecked or unsafe operations."

我需要担心这个吗?

我做得对吗?

MacBook-Pro-3:Documents garrettsmith$ javac codepoop/*.java -Xlint:unchecked
codepoop/GArrayList.java:30: 警告: [unchecked] 未经检查的转换
    return (T) elements[i];
                       ^
  需要: T
  找到:    Object
  其中 T 是类型变量:
    T extends Object 在类 GArrayList 中声明
1 个警告

GArrayList:

import java.util.Arrays;

public class GArrayList<T> {

  private Object elements[];
  private int size = 0;

  public GArrayList() {
    elements = new Object[1];
  }

  public T get(int i) {
    return (T) elements[0]; // javac 对这一行发出警告。
  }
}
英文:

I'm a JavaScript developer doing a Java exercise and I could use help.

When I try to compile with javac, I get:
&quot;uses unchecked or unsafe operations.&quot;

Do I need to worry about that?

Am I doing this right?

MacBook-Pro-3:Documents garrettsmith$ javac codepoop/*.java -Xlint:unchecked
codepoop/GArrayList.java:30: warning: [unchecked] unchecked cast
    return (T) elements[i];
                       ^
  required: T
  found:    Object
  where T is a type-variable:
    T extends Object declared in class GArrayList
1 warning

GArrayList:

import java.util.Arrays;

public class GArrayList&lt;T&gt; {

  private Object elements[];
  private int size = 0;

  public GArrayList() {
    elements = new Object[1];
  }

  public T get(int i) {
    return (T) elements[0]; // javac complains about this line.
  }
}

答案1

得分: 1

你声明了一个泛型类,但你没有使用泛型类型 T 来存储数据。因为你使用了数组,实际上是无法这么做的。在这种情况下,你可以简单地抑制警告,只要确保你的类只会添加 T 类型的对象。

public class GArrayList<T> {

  private Object elements[];
  private int size = 0;

  public GArrayList() {
    elements = new Object[1];
  }

  @SuppressWarnings("unchecked")
  public T get(int i) {
    return (T) elements[0]; // javac complains about this line.
  }

  public void add(T element) {
    elements[index++] = element;
  }
}

注意,add() 方法只接收 T 类型,并且数组是私有的,所以无法从类外部访问它。但是,是的,add() 方法不会起作用。这只是为了展示如何通过自己的方式确保类型安全性。

英文:

You declared a generic class but you are not using the generic type T to store data. Because you are using an array you actually can't. In this case you can just suppress the warning provided that you class ensures only objects of type T are going to be added.

public class GArrayList&lt;T&gt; {

  private Object elements[];
  private int size = 0;

  public GArrayList() {
    elements = new Object[1];
  }

  @SuppressWarnings(&quot;unchecked&quot;)
  public T get(int i) {
    return (T) elements[0]; // javac complains about this line.
  }

  public void add(T element) {
    elements[index++] = element;
  }
}

Note the add() method receives only T and the array is private so it can't be accessed from outside the class. But, yeah, the add() method won't work. It's just for showing how you can ensure the type safety by yourself.

huangapple
  • 本文由 发表于 2020年9月16日 11:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63912568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定