index out of bounds exception. array

前端 未结 2 1382
青春惊慌失措
青春惊慌失措 2020-12-19 23:41

I am trying to add the items from my array into the listview but i always get an exception.

below is the code of how i add the items:



        
相关标签:
2条回答
  • 2020-12-19 23:59

    It should be

    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));
    }
    

    Let's say we have an ArrayList with a 5 items

    so accessing elements of ArrayList starts from index 0 to 4

    0 讨论(0)
  • 2020-12-20 00:05

    use

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

    OR

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

    instead of

    for(int a = 0; a <= presList.size(); a++)
    
    0 讨论(0)
提交回复
热议问题