错误:通用数组创建

发布于 2024-09-26 20:38:09 字数 730 浏览 5 评论 0原文

我不明白通用数组创建的错误。
首先我尝试了以下方法:

  public PCB[] getAll() {
       PCB[] res = new PCB[list.size()];
           for (int i = 0; i < res.length; i++) {
               res[i] = list.get(i);
            }
       list.clear();
       return res;
}


然后我尝试这样做:

PCB[] res = new PCB[100];


我一定错过了一些看起来正确的东西。我试着查了一下,确实是这样。没有任何反应。


我的问题是:我能做些什么来解决这个问题?


错误是:

.\Queue.java:26: generic array creation
PCB[] res = new PCB[200];
            ^
Note: U:\Senior Year\CS451- file      
uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

工具已完成,退出代码为 1

I don't understand the error of Generic Array Creation.
First I tried the following:

  public PCB[] getAll() {
       PCB[] res = new PCB[list.size()];
           for (int i = 0; i < res.length; i++) {
               res[i] = list.get(i);
            }
       list.clear();
       return res;
}

Then I tried doing this:

PCB[] res = new PCB[100];

I must be missing something cause that seems right. I tried looking it up I really did. And nothing is clicking.

My question is: What can I do to fix this?

the error is :

.\Queue.java:26: generic array creation
PCB[] res = new PCB[200];
            ^
Note: U:\Senior Year\CS451- file      
uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Tool completed with exit code 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦境 2024-10-03 20:38:09

您无法使用通用组件类型创建数组。

相反,创建一个显式类型的数组,例如 Object[]。如果需要,您可以将其转换为 PCB[],但在大多数情况下我不建议这样做。

PCB[] res = (PCB[]) new Object[list.size()]; /* Not type-safe. */

如果您想要类型安全,请使用像 java.util.List这样的集合而不是数组。

顺便说一句,如果 list 已经是 java.util.List,您应该使用它的 toArray() 方法之一,而不是重复它们在你的代码中。但这并不能解决类型安全问题。

You can't create arrays with a generic component type.

Create an array of an explicit type, like Object[], instead. You can then cast this to PCB[] if you want, but I don't recommend it in most cases.

PCB[] res = (PCB[]) new Object[list.size()]; /* Not type-safe. */

If you want type safety, use a collection like java.util.List<PCB> instead of an array.

By the way, if list is already a java.util.List, you should use one of its toArray() methods, instead of duplicating them in your code. This doesn't get you around the type-safety problem though.

岛徒 2024-10-03 20:38:09

下面将为您提供所需类型的数组,同时保持类型安全。

PCB[] getAll(Class<PCB[]> arrayType) {  
    PCB[] res = arrayType.cast(java.lang.reflect.Array.newInstance(arrayType.getComponentType(), list.size()));  
    for (int i = 0; i < res.length; i++)  {  
        res[i] = list.get(i);  
    }  
    list.clear();  
    return res;  
}

我的回答深入解释了它的工作原理柯克·沃尔作为重复项链接的问题。

The following will give you an array of the type you want while preserving type safety.

PCB[] getAll(Class<PCB[]> arrayType) {  
    PCB[] res = arrayType.cast(java.lang.reflect.Array.newInstance(arrayType.getComponentType(), list.size()));  
    for (int i = 0; i < res.length; i++)  {  
        res[i] = list.get(i);  
    }  
    list.clear();  
    return res;  
}

How this works is explained in depth in my answer to the question that Kirk Woll linked as a duplicate.

°如果伤别离去 2024-10-03 20:38:09

除了“可能的重复”中建议的方法之外,解决此问题的另一种主要方法是由调用者提供数组本身(或至少一个模板),调用者希望知道具体类型并且可以从而安全地创建数组。

这是ArrayList.toArray(T[]) 等方法的实现方式。我建议您看一下该方法以获取灵感。更好的是,正如其他人指出的那样,您可能应该使用该方法。

Besides the way suggested in the "possible duplicate", the other main way of getting around this problem is for the array itself (or at least a template of one) to be supplied by the caller, who will hopefully know the concrete type and can thus safely create the array.

This is the way methods like ArrayList.toArray(T[]) are implemented. I'd suggest you take a look at that method for inspiration. Better yet, you should probably be using that method anyway as others have noted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文