[英]ArrayList - how can I check if an index exists?
我正在使用ArrayList<String>
并在特定索引处添加数据,如何检查特定索引是否存在?
我应该简单地get()
并检查值吗? 还是我应该等待异常? 还有其他方法吗?
更新:感谢您的回答,但由于我只在特定索引处添加内容,因此列表的长度不会显示哪些可用。
方法arrayList.size()
返回列表中的项目数 - 所以如果索引大于或等于size()
,它不存在。
if(index >= myList.size() || index < 0){
//index does not exists
}else{
// index exists
}
虽然您收到了十几个关于使用列表大小的建议,这些建议适用于具有线性条目的列表,但似乎没有人阅读您的问题。
如果您在不同的索引处手动添加条目,这些建议都不起作用,因为您需要检查特定索引。
使用if ( list.get(index) == null )也不起作用,因为 get() 会抛出异常而不是返回 null。
尝试这个:
try {
list.get( index );
} catch ( IndexOutOfBoundsException e ) {
list.add( index, new Object() );
}
如果索引不存在,则在此处添加一个新条目。 你可以改变它来做一些不同的事情。
这就是你需要的...
public boolean indexExists(final List list, final int index) {
return index >= 0 && index < list.size();
}
为什么不使用普通的旧数组? 我认为对 List 的索引访问是一种代码味道。
通常我只是检查索引是否小于数组大小
if (index < list.size()) {
...
}
如果您还担心索引是负值,请使用以下
if (index >= 0 && index < list.size()) {
...
}
关于您的更新(这可能应该是另一个问题)。 您应该使用这些对象的数组而不是 ArrayList,因此您可以简单地检查 null 的值:
Object[] array = new Object[MAX_ENTRIES];
..
if ( array[ 8 ] == null ) {
// not available
}
else {
// do something
}
最佳实践
如果您的数组中没有数百个条目,则应考虑将其组织为一个类以摆脱魔术数字 3,8 等。
使用异常的控制流是不好的做法。
这是为Kotlin开发人员准备的:
if (index in myList.indices) {
// index is valid
}
其他解决方案:
if (index in 0..myArray.lastIndex) {
// index is valid
}
if (index >= 0 && index <= myList.lastIndex) {
// index is valid
}
// Note: elements of the list should be non-null
if (myList.getOrNull(index) != null) {
// index is valid
}
// Note: elements of the list should be non-null
myList.getOrNull(index)?.let { element ->
// index is valid; use the element
}
从 Java 9 开始,有一种检查索引是否属于数组的标准方法 - Objects#checkIndex() :
List<Integer> ints = List.of(1,2,3);
System.out.println(Objects.checkIndex(1,ints.size())); // 1
System.out.println(Objects.checkIndex(10,ints.size())); //IndexOutOfBoundsException
您可以使用size()
方法检查ArrayList
的大小。 这将返回最大索引 +1
快速而肮脏的测试索引是否存在。 在您的实施替换列表中使用您正在测试的列表。
public boolean hasIndex(int index){
if(index < list.size())
return true;
return false;
}
或者对于二维数组列表...
public boolean hasRow(int row){
if(row < _matrix.size())
return true;
return false;
}
您可以检查数组的大小。
package sojava;
import java.util.ArrayList;
public class Main {
public static Object get(ArrayList list, int index) {
if (list.size() > index) { return list.get(index); }
return null;
}
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(""); list.add(""); list.add("");
System.out.println(get(list, 4));
// prints 'null'
}
}
一个简单的方法来做到这一点:
try {
list.get( index );
}
catch ( IndexOutOfBoundsException e ) {
if(list.isEmpty() || index >= list.size()){
// Adding new item to list.
}
}
如果您的索引小于列表的大小,则它确实存在,可能具有null
值。 如果索引更大,那么您可以调用ensureCapacity()
来使用该索引。
如果要检查索引处的值是否为null
,请调用get()
Scanner input = new Scanner(System.in);
System.out.println("Enter the index to check");
int i = input.nextInt();
if (i > tasksList.size() || i < 0 ){
System.out.println("invalid input");
}else {
//your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.