How to clear a list one by one in Java?

Hi,
I would like to clear a list in Java as I loop through it.

This is how I loop through:

        for (CheckDetail checkDetail : checkDetails) {
            TableItem item = new TableItem(table, SWT.NULL);
            //item.setText(0, checkDetail.getStatus()); //Override
            //item.setText(1, checkDetail.getStatus()); //Rerun
            item.setText(0, checkDetail.getStatus()); //Status
            item.setText(1, checkDetail.getName()); //Check
            item.setText(2, checkDetail.getFileName()); //Filename
            item.setText(3, checkDetail.getExistingLineNumbers()); //Existing Line Numbers
            item.setText(4, checkDetail.getNewLineNumbers()); //New Line Numbers
            
            //This is to make shadow for every other line
            if (evenRow)
                item.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
            evenRow = !evenRow;
         }

Open in new window



Each time I get the information from one element I would like to remove it from the list. So , at the end when the for loop is complete I want the list to be empty.


How can I do it?
TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MisbahCommented:
generally speaking you can not remove an item inside such this kind of loop,
try to use the index to loop over the list.

for(int i=0; i < checkDetails.getLength(); i++){ .... }
0
CEHJCommented:
I would tend to use an Iterator explicitly


for(Iterator<CheckDetail> i = checkDetails.iterator();i.hasNext();) {
   CheckDetail CheckDetail = i.next();
   // The same as before
   i.remove(); // clear
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sharon SethCommented:
Perhaps using an iterator is the only suggested way of  removing items from a list whilst iterating over it . The behaviour is unpredictable if you do it other ways . I remember when I was not using an iterator and modifying a list , it would end up with a ConcurrentModificationException
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

TolgarAuthor Commented:
Perfect solution!
0
CEHJCommented:
You can for-loop it backwards but i don't like it

for(int i = checkDetails.size();--i>=0;) {
   CheckDetail cd = checkDetails.get(i);
   // Do it
   checkDetails.remove(i);
}

Open in new window

and anyway, you probably don't want to process in reverse order
0
CEHJCommented:
:)
0
TolgarAuthor Commented:
Thanks for the comments. I preferred to use the iterator.
0
CEHJCommented:
Much better
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.