java.lang.IndexOutOfBoundsException while using remove()


 if the chargeList ia have 162,620,045,620 chargeid's are there;i want to remove 620 chargeid's in the chargelist;when i am doing like below;but iam getting
 IndexOutOfBoundsException
 
 if(chargesList!=null && chargesList.size()>0)
        {
            HouseDocumentCharges houseDocumentCharges = null;
             for(int i=0;i<chargesList.size();i++)
                 {
                      houseDocumentCharges = (HouseDocumentCharges)chargesList.get(i);
                     if(houseDocumentCharges.chargeId.equals("620"))
                      cloneList.remove(i);
                 }
 }

 05/05/05 15:39:58 java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
05/05/05 15:39:58       at java.util.ArrayList.RangeCheck(ArrayList.java:507)
05/05/05 15:39:58       at java.util.ArrayList.get(ArrayList.java:324)
05/05/05 15:39:58       at com.foursoft.etrans.servlets.common.ETFinancialCorrectionController.validateChargesInfo(ETFinancialCorrectionController.java:3327)
05/05/05 15:39:58       at com.foursoft.etrans.servlets.common.ETFinancialCorrectionController.doPost(ETFinancialCorrectionController.java:714)
05/05/05 15:39:58       at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
05/05/05 15:39:58       at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
05/05/05 15:39:58       at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:65)
05/05/05 15:39:58       at com.foursoft.etrans.common.filter.CredentialFilter.doFilter(CredentialFilter.java:116)
05/05/05 15:39:58       at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:604)
05/05/05 15:39:58       at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:317)
05/05/05 15:39:58       at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:790)
05/05/05 15:39:58       at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:270)
05/05/05 15:39:58       at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:112)
05/05/05 15:39:58       at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:192)
05/05/05 15:39:59       at java.lang.Thread.run(Thread.java:534)
LVL 20
chaitu chaituAsked:
Who is Participating?
 
CEHJCommented:
>>houseDocumentCharges = (HouseDocumentCharges)it.next();

should be

houseDocumentCharges = (HouseDocumentCharges)j.next();

and remove the other iterator
0
 
CEHJCommented:
>>cloneList.remove(i);

You must check that this element exists first
0
 
chaitu chaituAuthor Commented:
sorry this is my code

 if(chargesList!=null && chargesList.size()>0)
        {
            HouseDocumentCharges houseDocumentCharges = null;
              ArrayList cloneList = (ArrayList) chargesList.clone();
              int size =cloneList.size();
             for(int i=0;i<size;i++)
                 {
                      houseDocumentCharges = (HouseDocumentCharges)cloneList.get(i);
                     if(houseDocumentCharges.chargeId.equals("620"))
                      cloneList.remove(houseDocumentCharges);
                 }

}
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
CEHJCommented:
>>i want to remove 620 chargeid's in the chargelist

You mean you want to remove HouseDocumentCharges with that id in the cloned list i think ...
0
 
chaitu chaituAuthor Commented:
inplace of   cloneList.remove(houseDocumentCharges) i put remove(i) still getting same error
0
 
chaitu chaituAuthor Commented:
yes
0
 
CEHJCommented:
You should probably take a deep copy i think:

 ArrayList cloneList = new ArrayList(chargesList);
0
 
CEHJCommented:
And you should really use an Iterator and its remove method
0
 
CEHJCommented:
Actually clone will work ok here. Check the following:


// Remove all elements in 'exclusions'
Set exclusions = new HashSet(Arrays.asList(new String[] {"B", "C"}));      
ArrayList original = new ArrayList(Arrays.asList(new String[] {"A", "B", "C", "D", "E"}));
ArrayList copy = (ArrayList)original.clone();
Iterator iter = copy.iterator();
while(iter.hasNext()) {
      if (exclusions.contains(iter.next())) {
            iter.remove();
      }
}
System.out.println(original);
System.out.println(copy);
0
 
chaitu chaituAuthor Commented:
if(chargesList!=null && chargesList.size()>0)
        {
            HouseDocumentCharges houseDocumentCharges = null;
              ArrayList cloneList = (ArrayList) chargesList.clone();
             Iterator it = cloneList.iterator();
             
                  for(Iterator j=cloneList.iterator();j.hasNext();)
                 {
                      houseDocumentCharges = (HouseDocumentCharges)it.next();
                     if(houseDocumentCharges.chargeId.equals("620"))
                      it.remove();
                 }
}

i have changed my code like this but ia m getting this exception

05/05/05 16:04:50 java.util.NoSuchElementException
05/05/05 16:04:50       at java.util.AbstractList$Itr.next(AbstractList.java:426)
05/05/05 16:04:50       at com.foursoft.etrans.servlets.common.ETFinancialCorrectionController.validateChargesInfo(ETFinancialCorrectionController.java:3329)
05/05/05 16:04:50       at com.foursoft.etrans.servlets.common.ETFinancialCorrectionController.doPost(ETFinancialCorrectionController.java:715)
05/05/05 16:04:50       at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
05/05/05 16:04:50       at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
05/05/05 16:04:50       at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:65)
05/05/05 16:04:50       at com.foursoft.etrans.common.filter.CredentialFilter.doFilter(CredentialFilter.java:116)
05/05/05 16:04:50       at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:604)
05/05/05 16:04:50       at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:317)
05/05/05 16:04:50       at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:790)
05/05/05 16:04:50       at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:270)
05/05/05 16:04:50       at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:112)
05/05/05 16:04:50       at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:192)
05/05/05 16:04:50       at java.lang.Thread.run(Thread.java:534)
0
 
objectsCommented:

if(chargesList!=null && chargesList.size()>0)
        {
            HouseDocumentCharges houseDocumentCharges = null;
              ArrayList cloneList = (ArrayList) chargesList.clone();
            cloneList.remove(cloneList.indexOf(new HouseDocumentCharges("620")));
}

 
0
 
objectsCommented:
of if you want to remove more than 1 element use:

if(chargesList!=null && chargesList.size()>0)
{
            ArrayList cloneList = (ArrayList) chargesList.clone();
            HouseDocumentCharges toremove = new HouseDocumentCharges("620");
            int index = 0;
            while (-1!=(index = cloneList.indexOf(toremove)))
            {
                cloneList.remove(index);
            }
}
0
 
CEHJCommented:
And of course

>> it.remove();

should be

j.remove();
0
 
CEHJCommented:
:-)

>> of if you want to remove more than 1 element use:

The code already removes more than one element if more than one is present
0
 
objectsCommented:
(:
0
 
chaitu chaituAuthor Commented:
i think we can also do like this

 Set exclusions = new HashSet(Arrays.asList(new String[] {"620"}));    
      ArrayList al = new ArrayList();
      al.add("162");
      al.add("176");
      al.add("666");
      al.add("620");
      al.add("205");
      al.add("620");
      ArrayList original = new ArrayList(al);
      ArrayList copy = (ArrayList)original.clone();
      Iterator iter = copy.iterator();
   
    while(iter.hasNext()) {
         if (exclusions.contains(iter.next())) {
              iter.remove();
         }
    }
   
System.out.println(original);
System.out.println(copy);
0
 
CEHJCommented:
This will suffice:


Set exclusions = new HashSet();    
exclusions.add("162");
exclusions.add("176");
exclusions.add("666");
exclusions.add("205");
exclusions.add("620");
ArrayList copyOfYourChargesList = (ArrayList)yourChargesList.clone();
Iterator iter = copyOfYourChargesList.iterator();
while(iter.hasNext()) {
      if (exclusions.contains(iter.next())) {
            iter.remove();
      }
}
System.out.println(yourChargesList);
System.out.println(copyOfYourChargesList);

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.