[英]In Java is there a way to name an ArrayList inside the ArrayList?
When I've needed a way of easily identifying an ArrayList inside an ArrayList, I would just put an identifier in the 0 index, then print out all the zero indexes with a loop. 当我需要一种轻松识别ArrayList中的ArrayList的方法时,我只需将标识符放在0索引中,然后用循环打印出所有零索引。 Now I'm wondering if there is a way to make the nested ArrayLists have names.
现在,我想知道是否有办法使嵌套的ArrayList具有名称。
for example: 例如:
ListOfLists.add( new ArrayList<String>() );
ListOfLists.get(ListOfLists.size() -1).add("someIdentifierHumansCanRead");
would there be some way to make ListOfLists.add(new ArrayList<String>())
be ListOfLists.add(ArrayList someIdentifierHumansCanRead = new ArrayList<String>)
? 有什么办法可以使
ListOfLists.add(new ArrayList<String>())
成为ListOfLists.add(ArrayList someIdentifierHumansCanRead = new ArrayList<String>)
吗?
Or would this just be completely absurd? 还是这完全是荒谬的?
You are speaking of map where all the array lists are keyed by strings - but you will lose iteration order. 您所说的map是所有数组列表都由字符串作为键的映射-但是您将失去迭代顺序。 Alternatively you can store keys in array list and array lists in maps.
或者,您可以将键存储在数组列表中,而可以将键存储在映射中。
Implement your own ArrayList
class and define an identifier property for it: 实现您自己的
ArrayList
类并为其定义一个标识符属性:
class NamedList<T> extends ArrayList
{
private String name;
public NamedList(String name) {
this.name = name;
}
// Getters/Setters and other stuff for identifier, etc.
}
ArrayList<NamedList<String>> superList = new ArrayList<NamedList<String>>();
superList.add(new NamedList<String>("babangida"));
If you need to "name" your ArrayList, then you really want to create a meaningful data structure instead of using hacks. 如果您需要“命名” ArrayList,那么您真的想创建一个有意义的数据结构而不是使用hack。 This is OO programming, after all:
毕竟,这是OO编程:
public void MyNamedBean {
private String name;
private final List<String> list = new ArrayList<String>();
public MyNamedBean(String name) {
this.name = name;
}
public String getName() {
return name;
}
// If you don't need to change the name, make the field
// final and remove this method.
public void setName(String name) {
this.name = name;
}
// It should have a more meaningful name.
public List<String> getList() {
return list;
}
}
Update, based on my comments on another answer: it's an OO design issue. 根据我对另一个答案的评论进行更新:这是一个OO设计问题。 If it's a list with a name, it's not just a list, which means you shouldn't directly extend a List implementation, but instead create a meaningful class.
如果它是一个带有名称的列表,则不仅仅是一个列表,这意味着您不应该直接扩展List实现,而应该创建一个有意义的类。 What if you need at some point a second list, or another "name" (a human-readable label, for example)?
如果您有时需要第二个列表或另一个“名称”(例如,人类可读的标签)怎么办? You can read on the Liskov substitution principle about when to use inheritance and when to use composition.
您可以阅读有关何时使用继承和何时使用合成的Liskov替换原理 。 See also Prefer composition over inheritance .
另请参见优先于继承而不是继承 。
If you meant references you cant try this 如果您的意思是引用,则无法尝试此操作
ArrayList someIdentifierHumansCanRead = new ArrayList<String>();
ListOfLists.add(someIdentifierHumansCanRead)
Or you can use a Map<String,List<String>>
if you want to retrieve the arraylist using an identifier. 或者
Map<String,List<String>>
如果要使用标识符检索Map<String,List<String>>
可以使用Map<String,List<String>>
。
map.put("id",new ArrayList<String>());
The map can be a HashMap / LinkedHashMap / TreeMap etc. The iteration order will depend on the chosen implementation. 该映射可以是HashMap / LinkedHashMap / TreeMap等。迭代顺序将取决于所选的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.