java.lang.IndexOutOfBoundsException:索引:4,大小:4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3882014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:19:11  来源:igfitidea点击:

java.lang.IndexOutOfBoundsException: Index: 4, Size: 4

javaarraylist

提问by gary A.K.A. G4

How can I fix this OutOfBoundsException?

我该如何解决这个问题OutOfBoundsException

Here is the code I am using:

这是我正在使用的代码:

 ResultSet rsTagCheck = stmt.executeQuery(
     "SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS, PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'");
 while (rsTagCheck.next()){
     String TagNum = rsTagCheck.getString("XKRPRMT_TAG");
     ArrayList<String> myTag = new ArrayList<String>();

     for (int i = 0; i < TagNum.length(); i++){
         myTag.add(TagNum);
         myTag.get(i + i);

I kinda know why I am getting the error, but I am not sure how to remedy the problem.

我有点知道为什么我会收到错误消息,但我不确定如何解决这个问题。

采纳答案by Pointy

What is it that you expect myTag.get(i + i)to do?

你期望myTag.get(i + i)做什么?

The first time through the loop, "i" is zero and you add one element. There won't be an element 1, so the call will throw an exception.Now that I actually see what you wrote, it'll fail on the second iteration, not the first, as poor @Giu noted in his now-deleted answer. Still, it's weird and I don't know what you're trying to accomplish by calling .get()and not even looking at the return value.

第一次循环时,“i”为零,您添加一个元素。不会有元素 1,所以调用会抛出异常。现在我真的看到了你写的东西,它会在第二次迭代中失败,而不是第一次,正如可怜的@Giu 在他现在删除的答案中指出的那样。尽管如此,这很奇怪,我不知道您要通过调用.get()甚至不查看返回值来完成什么。

You really will have to explain what it is you're trying to do, because that doesn't really make any sense as written. Did the exception in the question title reallycome from that code, or did you edit part of it out when posting?

你真的必须解释你想要做什么,因为这实际上没有任何意义。问题标题中的异常是否真的来自该代码,还是您在发布时对其进行了部分编辑?

edit— whoops totally saw "i+i" as "i+1". Still makes no sense to me however.

编辑— 哎呀,完全将“i+i”视为“i+1”。然而,对我来说仍然没有意义。

回答by Giuseppe Accaputo

The problem is the i+ipart in myTag.get(i+i). It'll work for i=0, but as soon as i=1, you'll get an exception thrown, since you've added two elements to myTag, but are accessing the third element (myTag.get(2)).

问题是i+i部分myTag.get(i+i)。它适用于i=0,但是一旦i=1,您就会抛出异常,因为您已向 中添加了两个元素myTag,但正在访问第三个元素 ( myTag.get(2))。

回答by codaddict

This myTag.get(i + i);is causing the exception.

myTag.get(i + i);导致了异常。

First time in the loop iis 0, you add an item into the ArrayList and then call get(0+0)which is fine.

循环中的第一次i0,您将一个项目添加到 ArrayList 中,然后调用get(0+0)这很好。

In the next iteration, you add another element(total of 2 element in the list now) and call get(1+1), this causes exception as you have only 2elements and valid index are 0and 1.

在下一次迭代中,您添加另一个元素(现在列表中共有 2 个元素)并调用get(1+1),这会导致异常,因为您只有2元素和有效索引是01

回答by virgium03

You are using the for loop by iterating on the String TagNum. You should only need to say: myTag.add(TagNum).

您正在通过迭代 String TagNum 来使用 for 循环。你应该只需要说:myTag.add(TagNum)

Imagine that the String TagNum has 4 characters. You add the String to the list 4 times, but when you reach i = 3, you are trying to retrieve the element at position 3 + 1, but the list has elements from 0 to 3.

假设 String TagNum 有 4 个字符。您将字符串添加到列表 4 次,但是当您达到 i = 3 时,您尝试检索位置 3 + 1 处的元素,但列表具有从 0 到 3 的元素。

Also, try replacing the BannerID with a ? and set the parameter to the statement accordingly.

另外,尝试用 ? 并相应地将参数设置为语句。

回答by Jay

Even without the problem with the get, your program as written will read through the results of the query, and then for each CHARACTER in tagNum, it will add an instance of tagNum to your array. So if tagNum is, say, "ABC", the array will end up containing "ABC" three times. If tagNum is "ABCD", it will contain "ABCD" four times. This doesn't make a lot of sense.

即使 get 没有问题,您编写的程序也会读取查询的结果,然后对于 tagNum 中的每个 CHARACTER,它会将 tagNum 的一个实例添加到您的数组中。因此,如果 tagNum 是“ABC”,则数组最终将包含“ABC”三次。如果 tagNum 是“ABCD”,它将包含四次“ABCD”。这没有多大意义。

I think what you want is to just add tagNum to an array, defining the array OUTSIDE of the ResultSet.next loop. Something like this maybe:

我认为你想要的是将 tagNum 添加到一个数组中,定义 ResultSet.next 循环的 OUTSIDE 数组。可能是这样的:

 ArrayList<String> myTag = new ArrayList<String>(); 
ResultSet rsTagCheck = stmt.executeQuery( 
  "SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS,   PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'"); 
while (rsTagCheck.next()){ 
  String TagNum = rsTagCheck.getString("XKRPRMT_TAG"); 
  myTag.add(TagNum); 
}

(Of course this doesn't use any of the other data in your query and I don't know what all else you're up to, but I believe that's what you're trying to do for this part.)

(当然,这不会使用您查询中的任何其他数据,我不知道您在做什么,但我相信这就是您要为这部分做的事情。)

Update

更新

Suppose you have ten records in your database table. After the above loop is complete, the array should be populated.

假设您的数据库表中有 10 条记录。完成上述循环后,应填充数组。

Try something like this:

尝试这样的事情:

ArrayList<String> myTag = new ArrayList<String>(); 
ResultSet rsTagCheck = stmt.executeQuery( 
  "SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS,   PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'"); 
while (rsTagCheck.next()){ 
  String TagNum = rsTagCheck.getString("XKRPRMT_TAG"); 
  myTag.add(TagNum); 
}
for (String tag : myTag)
{
  System.out.println(tag);
}

That should give you the list of all the tags. Note you have to examine the List AFTER the while(ResultSet) loop ends. Inside the loop you will only have the elements read so far.

那应该给你所有标签的列表。请注意,您必须在 while(ResultSet) 循环结束后检查 List。在循环内,您将只读取到目前为止的元素。

If you're still getting only one value, make sure that you have more than one record coming back from the result set. Like, run the query outside of a Java program and see how many records you get.

如果您仍然只获得一个值,请确保您从结果集中返回了多个记录。比如,在 Java 程序之外运行查询,看看你得到了多少记录。

回答by Dana

List<WebElement> div1=driver.findElements(By.xpath(".//*[@class='art_title']"));        

for(int i=0;i<=div1.size();i++)
{           
    System.out.println(div1.get(i).getText());          
    Thread.sleep(1000);
}


Instead of the above format I changed it into this format : 


List<WebElement> div1=driver.findElements(By.xpath(".//*[@class='art_title']"));
String[] abc = new String[div1.size()];

int i= 0;

for (WebElement e : div1) 
{
        abc[i] = e.getText();
        i++;            
        System.out.println(e.getText());
}