[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java.lang.IndexOutOfBoundsException while using remove()

Posted on 2005-05-05
17
Medium Priority
?
325 Views
Last Modified: 2009-07-29

 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)
0
Comment
Question by:chaitu chaitu
  • 9
  • 5
  • 3
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13934123
>>cloneList.remove(i);

You must check that this element exists first
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13934132
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13934137
>>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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13934138
inplace of   cloneList.remove(houseDocumentCharges) i put remove(i) still getting same error
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13934139
yes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13934154
You should probably take a deep copy i think:

 ArrayList cloneList = new ArrayList(chargesList);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13934160
And you should really use an Iterator and its remove method
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13934199
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13934230
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
 
LVL 92

Expert Comment

by:objects
ID: 13934235

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

 
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 13934245
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 800 total points
ID: 13934251
>>houseDocumentCharges = (HouseDocumentCharges)it.next();

should be

houseDocumentCharges = (HouseDocumentCharges)j.next();

and remove the other iterator
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13934256
And of course

>> it.remove();

should be

j.remove();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13934322
:-)

>> 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
 
LVL 92

Expert Comment

by:objects
ID: 13940709
(:
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13943106
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13943326
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question