java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 in android : I have a list of items and for some purpose, I have divided that list into two more ArrayList based on some criteria. When I have added all items of the combined list into 2 separate lists then I am not getting any error but If I add some items of the combined list into 1 and some items into 2 then I am getting an error. Tell me how to resolve "IndexOuOfBoundError."



Code: Adding data from combined list to 2 lists


                if (modelRipeExpiryArrayList.size() > 0) {


            for (int i = 0; i < modelRipeExpiryArrayList.size(); i++) {


                if (!(modelRipeExpiryArrayList.get(i).getExpiryDate().equals("")) && modelRipeExpiryArrayList.get(i).getIsRipe() == 1) {


                    explist.add(modelRipeExpiryArrayList.get(i).getItemName() + "@" + modelRipeExpiryArrayList.get(i).getExpiryDate());

                    ripeList.add(modelRipeExpiryArrayList.get(i).getItemName());

                } else if ((modelRipeExpiryArrayList.get(i).getExpiryDate().equals("")) && modelRipeExpiryArrayList.get(i).getIsRipe() == 1) {

                    ripeList.add(modelRipeExpiryArrayList.get(i).getItemName());

                } else if (!(modelRipeExpiryArrayList.get(i).getExpiryDate().equals("")) && modelRipeExpiryArrayList.get(i).getIsRipe() == 0) {


                    explist.add(modelRipeExpiryArrayList.get(i).getItemName() + "@" + modelRipeExpiryArrayList.get(i).getExpiryDate());

                }


            }

            Log.e("TAG", "getInitialized: "+ripeList.size() +"((" +explist.size() );



            AdapterExpHome adapterExpHome = new AdapterExpHome(getActivity(), modelRipeExpiryArrayList,  explist, HomeFragment.this);
            RecyclerView.LayoutManager elayoutManager = new LinearLayoutManager(getActivity());
            exp_list.setLayoutManager(elayoutManager);
            exp_list.setItemAnimator(new DefaultItemAnimator());
            exp_list.setAdapter(adapterExpHome);
            adapterExpHome.notifyDataSetChanged();


            AdapterHomeRipe adapterHomeRipe = new AdapterHomeRipe(getActivity(), modelRipeExpiryArrayList,  ripeList, HomeFragment.this);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
            ripe_list.setLayoutManager(layoutManager);
            ripe_list.setItemAnimator(new DefaultItemAnimator());
            ripe_list.setAdapter(adapterHomeRipe);
            adapterHomeRipe.notifyDataSetChanged();




        }



Code for Adapter : 

            public class AdapterHomeRipe extends RecyclerView.Adapter<AdapterHomeRipe.MyViewHolder> {


    Context context;
    FragmentRipe fragmentRipe;
    ArrayList<ModelRipeExpiry> modelRipeExpiryArrayList;
    ModelRipeExpiry modelRipeExpiry;

    ArrayList<String> ripeList;

    int qty;


    public AdapterHomeRipe(Context context, ArrayList<ModelRipeExpiry> modelRipeExpiryArrayList,  ArrayList<String> ripeList, HomeFragment homeFragment) {
        this.context = context;
        this.modelRipeExpiryArrayList = modelRipeExpiryArrayList;

        this.ripeList = ripeList;
        this.fragmentRipe = fragmentRipe;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_ripe_home, parent, false);


        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {


        modelRipeExpiry = modelRipeExpiryArrayList.get(position);

        Log.e("TAG", "adapterRipe: "+ ripeList.get(position));


       // holder.txtRipeVeggie.setText(ripeList.get(position));


        if (position % 2 == 0) {


            holder.layRipe.setBackgroundColor(context.getResources().getColor(R.color.lightGrey));

        } else {


            holder.layRipe.setBackgroundColor(context.getResources().getColor(R.color.white));


        }


    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }


    @Override
    public int getItemCount() {
        return modelRipeExpiryArrayList.size();
    }

    private static class ViewHolder {


    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView  txtRipeVeggie;
        RelativeLayout layRipe;
        LinearLayout layMain;


        public MyViewHolder(View itemView) {
            super(itemView);



            layRipe = (RelativeLayout) itemView.findViewById(R.id.layRipe);
           txtRipeVeggie = (TextView) itemView.findViewById(R.id.txtRipeVeggie) ;

            layMain = (LinearLayout) itemView.findViewById(R.id.layMain) ;



        }
    }




}




