java list是否存在_java:ArrayList - 如何检查索引是否存在?

在Java中,要检查ArrayList的索引是否存在,可以使用`size()`方法。如果索引大于或等于`size()`,则索引不存在。例如,`if(index >= myList.size())`表示索引不存在。另一种方法是尝试使用`get()`方法,如果索引超出范围,它会抛出`IndexOutOfBoundsException`,然后可以捕获该异常以处理不存在的索引情况。
摘要由CSDN通过智能技术生成

java:ArrayList - 如何检查索引是否存在?

我正在使用get(),我在特定指数处添加数据,如何检查特定索引是否存在?

我应该只是get()并检查值? 或者我应该等待例外?还有另外一种方法吗?

更新

谢谢你的答案,但因为我只是在特定索引处添加内容,列表的长度不会显示哪些是可用的。

ufk asked 2019-09-14T21:00:27Z

11个解决方案

126 votes

方法arrayList.size()返回列表中的项目数 - 因此,如果索引大于或等于size(),则它不存在。

if(index >= myList.size()){

//index not exists

}else{

// index exists

}

Amarghosh answered 2019-09-14T21:00:42Z

64 votes

虽然你有十几条关于使用列表大小的建议,这些建议适用于带有线性条目的列表,但似乎没有人能够阅读你的问题。

如果您在不同的索引处手动添加条目,则这些建议都不起作用,因为您需要检查特定索引。

使用if(list.get(index)== null)也不起作用,因为get()抛出异常而不是返回null。

试试这个:

try {

list.get( index );

} catch ( IndexOutOfBoundsException e ) {

list.add( index, new Object() );

}

如果索引不存在,则添加新条目。 你可以改变它来做一些不同的事情。

pli answered 2019-09-14T21:01:27Z

12 votes

这就是你需要的......

public boolean indexExists(final List list, final int index) {

return index >= 0 && index < list.size();

}

为什么不使用普通的旧阵列? 对List的索引访问是我认为的代码味道。

Paul McKenzie answered 2019-09-14T21:01:58Z

7 votes

关于你的更新(可能应该是另一个问题)。您应该使用这些对象的数组而不是ArrayList,所以你可以简单地检查null的值:

Object[] array = new Object[MAX_ENTRIES];

..

if ( array[ 8 ] == null ) {

// not available

}

else {

// do something

}

最佳实践

如果你的阵列中没有数百个条目,你应该考虑将它组织成一个类来摆脱神奇的数字3,8等。

使用异常控制流是不好的做法。

stacker answered 2019-09-14T21:02:41Z

5 votes

通常我只是检查索引是否小于数组大小

if (index < list.size()) {

...

}

如果您还担心索引是负值,请使用以下内容

if (index >= 0 && index < list.size()) {

...

}

AamirR answered 2019-09-14T21:03:12Z

3 votes

您可以使用size()方法检查ArrayList的大小。 这将返回最大索引+1

jwoolard answered 2019-09-14T21:03:36Z

2 votes

自java-9以来,有一种检查索引是否属于数组的标准方法 - Objects#checkIndex():

List ints = List.of(1,2,3);

System.out.println(Objects.checkIndex(1,ints.size())); // 1

System.out.println(Objects.checkIndex(10,ints.size())); //IndexOutOfBoundsException

Anton Balaniuc answered 2019-09-14T21:04:01Z

1 votes

快速和脏的测试索引是否存在。 在您的实现替换列表中使用您正在测试的列表。

public boolean hasIndex(int index){

if(index < list.size())

return true;

return false;

}

或者用于2Dimensional ArrayLists ......

public boolean hasRow(int row){

if(row < _matrix.size())

return true;

return false;

}

t3dodson answered 2019-09-14T21:04:32Z

0 votes

如果您的索引小于列表的大小,则它确实存在,可能具有null值。 如果index更大,那么您可以调用get()以便能够使用该索引。

如果您想检查索引处的值是否为null,请致电get()

Dmitry answered 2019-09-14T21:05:04Z

0 votes

您可以检查数组的大小。

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'

}

}

miku answered 2019-09-14T21:05:29Z

0 votes

一个简单的方法:

try {

list.get( index );

}

catch ( IndexOutOfBoundsException e ) {

if(list.isEmpty() || index >= list.size()){

// Adding new item to list.

}

}

Josué answered 2019-09-14T21:05:55Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值