我们可以使用像数组索引这样的索引来访问List< T>吗? [英] Can we use index like array index to access List<T>?
本文介绍了我们可以使用像数组索引这样的索引来访问List< T>吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道我们是否可以使用索引访问列表
I am wondering if we can use index to access List
例如:
List<Integer> list;
list[5] //blah....
推荐答案
由于 []
是一个运算符而java不支持运算符重载,所以不能将它与List一起使用。相反,你必须使用 set(int index,T value)
和 get(int index)
方法,是冗长的,但提供完全相同的功能。
Since []
is an operator and java does not support operator overloading you can't use it with List. Instead you have to use the set(int index, T value)
and get(int index)
methods, which may be verbose but provide exact the same functionality.
这篇关于我们可以使用像数组索引这样的索引来访问List< T>吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文