英文:
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<T> implements MultiSet<T>, Iterable<T> {
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<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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论