Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

Question for CEHJ - LinkedList question

Hi:
   I have developed a method for removing every second employee from a list.
The code seems to work. Can you inspect and provide feedback?

import java.util.LinkedList;
import java.util.ListIterator;

public class Downsize
{  public static void main(String[] args)
   {  LinkedList staff = new LinkedList();
      staff.addLast("Dick");
      staff.addLast("Harry");
      staff.addLast("Romeo");
      staff.addLast("Tom");
      staff.addLast("Joe");
      staff.addLast("Mark");
      staff.addLast("Mary");
      staff.addLast("Harry");
      staff.addLast("Bill");
           
      ListIterator oldList = staff.listIterator();
      System.out.println("This is the old list:" + "\n");
     
      while (oldList.hasNext())
      {
         System.out.println(oldList.next());
      }
           
      downsize(staff);
     
      ListIterator updatedList = staff.listIterator();
      System.out.println("\n" + "This is the updated list:" + "\n");
      while (updatedList.hasNext())
      {
         System.out.println(updatedList.next());
      }  
       
   }
   
   public static void downsize(LinkedList staff)
   {  ListIterator iterator = staff.listIterator();      
      while (iterator.hasNext())
      {  
         int w = staff.indexOf(iterator.next());
         if (w != (staff.size() - 1))
         {  
            iterator.next();              
            iterator.remove();
         }
         
      }
                   
  }
   
}

0
LearningJava
Asked:
LearningJava
  • 3
1 Solution
 
CEHJCommented:
Yes, it seems OK. You can, of course, call the more lightweight iterator() if you're not going to modify the List, such as just printing it out.
0
 
LearningJavaAuthor Commented:
>>You can, of course, call the more lightweight iterator() >>if you're not going to modify the List, such as just >>printing it out.

Can you demonstrate with code?
0
 
CEHJCommented:
Change

>>ListIterator oldList = staff.listIterator();

to

Iterator oldList = staff.iterator();

and


>>ListIterator updatedList = staff.listIterator();


to

>>Iterator updatedList = staff.iterator();


0
 
CEHJCommented:
Thanks. The deletion could be done as follows:


 public static void downsize2(LinkedList staff) {
   ListIterator iterator = staff.listIterator();
   while (iterator.hasNext()) {
     iterator.next();
     if (iterator.hasNext()) {
       iterator.next();
       iterator.remove();
     }
   }
 }


0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now