扩展 ArrayList 并创建新方法
我在理解某些东西时遇到了一些问题 - 我可能会完全错误地处理这个问题。
我正在尝试创建一个扩展 ArrayList 的类,但有几种方法可以增加功能(至少对于我正在开发的程序而言)。
其中一种方法是 findById(int id),它在每个 ArrayList 对象中搜索特定的 id匹配。到目前为止它正在工作,但它不会让我做 for (Item i : this) { i.getId(); ?
我不明白为什么
完整代码:
public class CustomArrayList<Item> extends ArrayList<Item> {
// declare singleton instance
protected static CustomArrayList instance;
// private constructor
private CustomArrayList(){
// do nothing
}
// get instance of class - singleton
public static CustomArrayList getInstance(){
if (instance == null){
instance = new CustomArrayList();
}
return instance;
}
public Item findById(int id){
Item item = null;
for (Item i : this) {
if (i.getId() == id) {
// something
}
}
return item;
}
public void printList(){
String print = "";
for (Item i : this) {
print += i.toString() + "\n";
}
System.out.println(print);
}
}
I'm having a bit of a problem grasping something - I might be going about this completely wrong.
I am trying to create a class which extends ArrayList but has several methods which increase the functionality (at least for the program I am developing.)
One of the methods is a findById(int id), which searches each ArrayList object for a particular id match. So far it's working, but it won't let me do for (Item i : this) { i.getId(); }
I don't understand why?
Full code:
public class CustomArrayList<Item> extends ArrayList<Item> {
// declare singleton instance
protected static CustomArrayList instance;
// private constructor
private CustomArrayList(){
// do nothing
}
// get instance of class - singleton
public static CustomArrayList getInstance(){
if (instance == null){
instance = new CustomArrayList();
}
return instance;
}
public Item findById(int id){
Item item = null;
for (Item i : this) {
if (i.getId() == id) {
// something
}
}
return item;
}
public void printList(){
String print = "";
for (Item i : this) {
print += i.toString() + "\n";
}
System.out.println(print);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改
为
我怀疑
Item
是您要存储在列表中的类的名称。通过在CustomArrayList
之后添加
,您将引入一个隐藏此类的类型参数。使用
参数,您的代码显然不会总是有效,因为
T
可能引用任何类型。Change
to
I suspect
Item
is the name of the class that you want to store in the list. By adding<Item>
afterCustomArrayList
you're introducing a type parameter which shadows this class.With the
<Item>
parameter, your code is equal towhich obviously won't always work, as
T
may refer to any type.什么是
getId()
?据推测,它是某个类中的方法,但我们不知道哪个类。如果您实际上有一个名为
Item
的类,其中包含getId()
方法,这意味着它是一个列表,您只需阻止您的类被通用的。因此,您需要:
当前在您的类中,
Item
不引用名为 Item 的类,它引用名为Item< 的类型参数 /代码>。
现在就个人而言:
ArrayList< >
除非我真的不得不这样做,否则更喜欢组合而不是继承What is
getId()
? Presumably it's a method in some class, but we don't know which class.If you've actually got a class called
Item
with agetId()
method, which this is meant to be a list of, you simply need to stop your class from being generic. So instead of this:you want:
Currently within your class,
Item
doesn't refer to a class called Item, it refers to a type parameter calledItem
.Now personally:
ArrayList<>
unless I really had to, preferring composition over inheritance