gpt4 book ai didi

Java 泛型类对象数组实例化

转载 作者:行者123 更新时间:2023-11-29 09:59:46 24 4
gpt4 key购买 nike

<分区>

我有一个泛型类

public class GenericBookDet<K,V>
{
private K bkId;
private V bkDescription;

public GenericBookDet()
{
this.bkId = null;
this.bkDescription = null;
}

public GenericBookDet(K bkId, V bkDescription)
{
this.bkId = bkId;
this.bkDescription = bkDescription;
}
...
//getter methods
//setter methods
}

我在另一个类中创建了这个类的数组,想添加新书。

public class GenericBookStore<K,V> 
{
static int noOfbooks = 0;
GenericBookDet<K,V>bk1[];

public int addBook(K key, V bkDescription)
{
this.bk1[noOfbooks] = new GenericBookDet<K, V>(key,bkDescription);
this.bk1[noOfbooks].setbkDescription(bkDescription);

++noOfbooks;
return noOfbooks;
}
}

我按以下方式调用 addBook 方法:

GenericBookStore<String, String> bkStore = new GenericBookStore<String,String>();
bkStore.addBook("1010", "ABCD");

但这会在 addBook 方法的第一行抛出 NullPointerException。

我想在 GenericBookStore 中创建一个通用书籍数组,我可以在 bkDescription 字段中将 String 或对象传递给它;

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com