How do i use the bollean isEmpty on an array list

R

ron

How do I use the Boolean isEmpty test on an array list. While in a 'while
loop' ie continue till list is empty?
The name of the array list is pList.
Thanks for your help
 
J

Joona I Palaste

ron said:
How do I use the Boolean isEmpty test on an array list. While in a 'while
loop' ie continue till list is empty?
The name of the array list is pList.
Thanks for your help

Don't they teach programming at your school any more? Here is the
skeleton of the code you need to use.

do {
doMagicStuff(pList);
} while (!pList.isEmpty());

Note that if doMagicStuff() does not alter the contents of pList, then
your loop will run either exactly once, or perpetually. This is because
do...while loops themselves cannot change the state of ArrayLists.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"C++ looks like line noise."
- Fred L. Baube III
 
P

Phil Hanna

How do I use the Boolean isEmpty test on an array list. While in a 'while
loop' ie continue till list is empty?
The name of the array list is pList.
Thanks for your help

Does the homework assignment explicitly say you have to use the
isEmpty test? (I'm assuming that you are removing elements from the
list somehow). Or can you use anything that works?

If you just want to iterate over the list, the easiest thing to do is:

for (Iterator it = pList.iterator(); it.hasNext(); ) {
Object obj = it.next();
// Do something with obj, casting it into
// the appropriate type, if necessary
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top