?
Solved

Question for CEHJ - LinkedList question

Posted on 2003-03-16
4
Medium Priority
?
447 Views
Last Modified: 2012-06-21
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
Comment
Question by:LearningJava
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Expert Comment

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

Author Comment

by:LearningJava
ID: 8146923
>>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
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 8146943
Change

>>ListIterator oldList = staff.listIterator();

to

Iterator oldList = staff.iterator();

and


>>ListIterator updatedList = staff.listIterator();


to

>>Iterator updatedList = staff.iterator();


0
 
LVL 86

Expert Comment

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

801 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