any advice on how I can do the loop to remove the nth element?

Go To StackoverFlow.com

1

Write a method, downsize, that will remove every nth element from a LinkedList of employee names.

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

public class LinkedListDemo
{

   public static void main(String[] args)
   {
      LinkedList<String> staff = new LinkedList<String>();
      staff.add("John");
      staff.add("Bob");
      staff.add("Richard");
      staff.add("Alice");
      staff.add("Jane");
      staff.add("Carlos");
      staff.add("Jose");
      staff.add("Maria");

      downsize(staff, 3);
      System.out.println(staff);
      System.out.println("Expected: [John, Bob, Alice, Jane, Jose, Maria]");

   }

   public static void downsize(LinkedList<String> employeeNames, int n)
   {
      ListIterator<String> iter = employeeNames.listIterator();

      for(int i=n; i<employeeNames.size(); i++)
      {
         iter.next();
      }

      iter.remove();


   }

}

I'm having some troubles to find a perfect loop that can remove any nth element in my ListIterator. thank you!

2012-04-04 22:33
by jesus Sandoval
Well, you'll need a counter to track the n, right? And every time that counter hits the number, you remove that element, and continue iterating from that point - Dave Newton 2012-04-04 22:36
okay cool. is there any other way i can do this but without using the ListIterator or do i need it always - jesus Sandoval 2012-04-04 22:39


3

This will remove every n-th element from the employeeNames LinkedList:

for(int i=0; i<employeeNames.size(); i++)
  {
     iter.next();
     if (i % n == 0) iter.remove();
  }
2012-04-04 22:36
by Max
You should not provide full answers to homework questions - only hints and guideline - amit 2012-04-04 22:39
thanks for the quick feedback. i have one more question about the code you just wrote. can it be done in a different way without using the ListIterator or it is the only way - jesus Sandoval 2012-04-04 22:41
Oops, I didn't realize it was a homework question (did not pay attention to the tags). @jesusSandoval: You don't have to instantiate the iterator, but since this is a LinkedList this is the most efficient way to implement this. You should think how to alternatively implement this using an ArrayList - Max 2012-04-04 22:42
On second thaught it might be ok in this specific case, since the OP already did try already, and just needed a help where and how to remove() the elements from iterator.. - amit 2012-04-04 22:46
Let me understand what you just said. so it is more efficient to implement the linkedList by using the Iterator. is it correct - jesus Sandoval 2012-04-04 22:48
Max the for loop was correct. but the if statement was not right it was like this: if((i+1)%n==0) iter.remove( - jesus Sandoval 2012-04-04 22:52
@jesusSandoval: An iterator is the best way to move through a LinkedList. You could also simply write for (int i = 0; i < employees.size(); i++) { if (i % n == 0) employees.remove(i); }, but this would be very inneficient. Do you see why - Max 2012-04-04 22:53
i think it was like this since we start at index - jesus Sandoval 2012-04-04 22:53
@jesusSandoval: Yes, you are right - Max 2012-04-04 22:54
anyways thank for you help because you gave the idea - jesus Sandoval 2012-04-04 22:58


0

Never ever modify a list that you are currently iterating over. That way madness lies.

public static List<String> downsize(List<String> employeeNames, int n) {
    Set<String> employeesToRemove = new HashSet<String>();
    for (int index = n; index < employeeNames.size(); index += n) {
        employeesToRemove.add(employeeNames.get(index);
    }
    employeeNames.removeAll(employeesToRemove);
}

If, however, you absolutely must iterate over the array, here is that madness also.

public static List<String> downsize(List<String> employeeNames, int n) {
    for (int index = employeeNames.size() -  (n -(employeeNames.size()  % n))  ; index >= 0; index -= n) {
        employeeNames.remove(index);
    }
    return employeeNames;
}
2012-04-04 23:10
by Mike Adler
This is a homework, so the implementation speed probably doesn't matter. But I think it is still important for the student to understand that your solution is not appropriate. 1) There is no problem in using Iterator.remove(). That's what it exists for. 2) Your solution, apart from being more complex and instatiating a utility Set, also runs in O(N^2) time for a LinkedList. The same is true of your second solution. Any get(), remove() or add() operation on a LinkedList runs in O(N) time.. - Max 2012-04-04 23:26
thank you for the hint @Max - jesus Sandoval 2012-04-05 04:37
Ads