创建一个自定义类的数组

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

Creating an array of a custom class

问题

我已经创建了一个名为ObjectContainer的自定义类,并且正试图创建一个包含这些对象的数组。然而,当我尝试使用new ObjectContainer[initialCapacity];来实例化数组时,它给我返回了一个"Generic Array creation error"的错误。因此,我在网上找到了一个建议,建议将一个对象转换给它,我已经这样做了:

(ObjectContainer[]) new Object[initialCapacity];

然而,现在我得到了另一个错误:

java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [LHashSet$ObjectContainer;
([Ljava.lang.Object;在加载程序'bootstrap'的java.base模块中;
[LHashSet$ObjectContainer;在加载程序'app'的未命名模块中)

我该如何创建一个ObjectContainer数组呢?

编辑:
使用

(ObjectContainer[]) new ObjectContainer[initialCapacity];

会给我返回一个"Generic array creation error"错误。

ObjectContainer类:

private class ObjectContainer extends Object {
    String object;
    private ObjectContainer(String object) {
        this.object = object;
    }
}

这在一个HashSet类中实现:

public class HashSet<T> implements MultiSet<T>, Iterable<T> {
    private ObjectContainer hashTable[];
    public LinkedMultiHashSet(int initialCapacity) {
        hashTable = (ObjectContainer[]) new ObjectContainer[initialCapacity];
    }
}
英文:

I have created a custom class called ObjectContainer, and am trying to create an array containing those objects. However, when i try to instantiate the array with new ObjectContainer[initialCapacity];
it gives me Generic Array creation error. I thus found an answer online that suggests casting an object to it, which i have done

(ObjectContainer[]) new Object[initialCapacity];

However, i now get another error

> java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [LHashSet$ObjectContainer;
([Ljava.lang.Object; is in module java.base of loader 'bootstrap';
[LHashSet$ObjectContainer; is in unnamed module of loader 'app')

What do i do to create an ObjectContainer array?

Edit:
Using

(ObjectContainer[]) new ObjectContainer[initialCapacity];

Gives me Generic array creation error.

ObjectContainer class:

private class ObjectContainer extends Object {
    String object;
    private ObjectContainer(String object) {
        this.object = object;
    }

This is implemented within a HashSet class:

public class HashSet&lt;T&gt; implements MultiSet&lt;T&gt;, Iterable&lt;T&gt; {
private ObjectContainer hashTable[];
public LinkedMultiHashSet(int initialCapacity) {

       hashTable = (ObjectContainer[]) new ObjectContainer[initialCapacity];
    }
}

答案1

得分: 2

替换

(ObjectContainer[]) new Object[initialCapacity];

new ObjectContainer[initialCapacity];

因为无法将 Object 强制转换为 ObjectContainer

编辑 1:
使用提供的类,以下代码可以编译通过

public class Main {

    public static void main(String args[]) {
        ObjectContainer[] containers = new ObjectContainer[3];
    }
}

class ObjectContainer extends Object {
    String object;

    private ObjectContainer(String object) {
        this.object = object;
    }
}

编辑 2:
以下代码可以编译通过

public class Main {

    public static void main(String args[]) {
        ObjectContainer[] containers = new ObjectContainer[3];
    }
}

class ObjectContainer extends Object {
    String object;

    private ObjectContainer(String object) {
        this.object = object;
    }
}

interface MultiSet<T> {}
class LinkedMultiHashSet<T> implements MultiSet<T>, Iterable<T> {
    private ObjectContainer hashTable[];
    public LinkedMultiHashSet(int initialCapacity) {
        hashTable = new ObjectContainer[initialCapacity];
    }

    @Override
    public Iterator<T> iterator() {
        return null;
    }
}
英文:

Replace

(ObjectContainer[]) new Object[initialCapacity];

by

new ObjectContainer[initialCapacity];

as Object cannot be cast to ObjectContainer

EDIT 1 :
With provided class, the following code does compile

public class Main {

    public static void main (String args[]) {
        ObjectContainer[] containers = new ObjectContainer[3];
    }
}

class ObjectContainer extends Object {
    String object;

    private ObjectContainer(String object) {
        this.object = object;
    }
}

EDIT 2 :
The following code does compile

public class Main {

    public static void main (String args[]) {
        ObjectContainer[] containers = new ObjectContainer[3];
    }
}

class ObjectContainer extends Object {
    String object;

    private ObjectContainer(String object) {
        this.object = object;
    }
}

interface MultiSet&lt;T&gt; {}
class LinkedMultiHashSet&lt;T&gt; implements MultiSet&lt;T&gt;, Iterable&lt;T&gt; {
    private ObjectContainer hashTable[];
    public LinkedMultiHashSet(int initialCapacity) {
        hashTable = new ObjectContainer[initialCapacity];
    }

    @Override
    public Iterator&lt;T&gt; iterator() {
        return null;
    }
}

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

发表评论

匿名网友

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

确定