我无法获取 ArrayList 中的元素

I can't get the elements in ArrayList

这里是我的代码 select 数组列表命名测试中的值我需要根据按钮按下事件获取数组列表中的值但是它显示下面给出的错误

bn.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            d4 = ex.getText().toString();
            int i=test.size();
           for(int j=0;j<=i;j++) {
               String s= test.get(j).toString();
               Toast.makeText(getApplicationContext(),s + " ", Toast.LENGTH_SHORT).show();
           }

        }
    });

错误信息如下

 04-04 16:38:29.551 28723-28723/com.example.web03.bulkmessenger E/MotionRecognitionManager: mSContextService = null
 04-04 16:38:29.551 28723-28723/com.example.web03.bulkmessenger E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@3e5f9413
 04-04 16:38:32.051 28723-28723/com.example.web03.bulkmessenger E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.web03.bulkmessenger, PID: 28723
 java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
 at java.util.ArrayList.get(ArrayList.java:308)
 at com.example.web03.bulkmessenger.MessengerActivity.onClick(MessengerActivity.java:127)
 at android.view.View.performClick(View.java:5246)
 at android.widget.TextView.performClick(TextView.java:10573)
 at android.view.View$PerformClick.run(View.java:21256)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:145)
 at android.app.ActivityThread.main(ActivityThread.java:6912)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

提前感谢您的帮助

IndexOutOfBoundsException 因为您调用了数组大小 +1 ..所以在 for 循环中删除 j<=i。

    int i=test.size();         // array size (i.e 10)

     for(int j=0;j<i;j++) {   //j<=i is wrong  because it execute 11th time.so remove =

           String s= test.get(j).toString();  //test.get(11) 
           Toast.makeText(getApplicationContext(),s + " ", Toast.LENGTH_SHORT).show();
       }

您收到 IndexOutOfBoundsException 错误,因为您已将 for 循环中的变量 j 设置为 "j<=i" 尝试将其设置为此

bn.setOnClickListener(new View.OnClickListener() {


    @Override
    public void onClick(View v) {
        d4 = ex.getText().toString();
        int i=test.size();
       for(int j=0;j<i;j++) {
           String s= test.get(j).toString();
           Toast.makeText(getApplicationContext(),s + " ", Toast.LENGTH_SHORT).show();
       }

    }
});

只需将您的 for 循环 更改为

 for(int j=0;j<test.size();j++) 

因为索引从 0 开始,但是 size() 方法 returns 我们的列表长度。 因此,如果您 运行 for loop until j <= test.size() 您将超出列表的索引

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2

IndexOutOfBoundsException 当程序尝试使用超出有效索引范围的值访问可索引集合中的值时抛出。

问题

 for(int j=0;j<=i;j++) { // == Usually any negative integer less than or equal to -1 and positive integer greater than or equal to the size of the object is an index which would be out of bounds.

解决方案

 for(int j=0;j<i;j++) {

数组在 for 循环的最后一次迭代期间超出范围,因为 i 的值为 test.size();使用 for-each 循环。