索引越界异常。排列

时间:2012-11-24 04:41:43

标签: android arrays

我正在尝试将我的数组中的项添加到listview中,但我总是得到一个例外。

下面是我如何添加项目的代码:

items.add(new SectionItem("President"));
                for(int a = 0; a <= presList.size(); a++)
                {
                    String name = presList.get(a);
                    String companyName = presList.get(a + 1);
                    Log.d("company name:", companyName);
                    items.add(new EntryItem(name,companyName));
                }

Logcat输出:

11-24 04:36:44.981: I/System.out(719): [潘国驹博士, 世界科技出版公司(董事主席)]
11-24 04:36:44.990: D/company name:(719): 世界科技出版公司(董事主席)
11-24 04:36:44.990: D/AndroidRuntime(719): Shutting down VM
11-24 04:36:44.990: W/dalvikvm(719): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-24 04:36:45.010: E/AndroidRuntime(719): FATAL EXCEPTION: main
11-24 04:36:45.010: E/AndroidRuntime(719): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
11-24 04:36:45.010: E/AndroidRuntime(719):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-24 04:36:45.010: E/AndroidRuntime(719):  at java.util.ArrayList.get(ArrayList.java:304)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.scba.organization.CouncilList$LoadAllMembers$1.run(CouncilList.java:192)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.app.Activity.runOnUiThread(Activity.java:4170)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.scba.organization.CouncilList$LoadAllMembers.onPostExecute(CouncilList.java:184)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.scba.organization.CouncilList$LoadAllMembers.onPostExecute(CouncilList.java:1)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.AsyncTask.finish(AsyncTask.java:602)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.AsyncTask.access$600(AsyncTask.java:156)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.os.Looper.loop(Looper.java:137)
11-24 04:36:45.010: E/AndroidRuntime(719):  at android.app.ActivityThread.main(ActivityThread.java:4424)
11-24 04:36:45.010: E/AndroidRuntime(719):  at java.lang.reflect.Method.invokeNative(Native Method)
11-24 04:36:45.010: E/AndroidRuntime(719):  at java.lang.reflect.Method.invoke(Method.java:511)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-24 04:36:45.010: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-24 04:36:45.010: E/AndroidRuntime(719):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:8)

使用

for(int a = 0; a <= presList.size()-1; a++)

OR

for(int a = 0; a < presList.size(); a++)

而不是

for(int a = 0; a <= presList.size(); a++)

答案 1 :(得分:4)

应该是

for(int a = 0; a < presList.size(); a++)
{
        String name = presList.get(a);
        String companyName = presList.get(a);
        Log.d("company name:", companyName);
        items.add(new EntryItem(name,companyName));
}

假设我们有ArrayList个5项

所以访问ArrayList的元素从索引0到4

开始