Advertisement
Guest User

iterators

a guest
Apr 24th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4.  
  5.  
  6. public class Example {
  7.  
  8. static LinkedList<String> l = new LinkedList<String>();
  9. /*For ith iterator:
  10. A B
  11. For jth iterator:
  12. A B C
  13. After remove is executed:the list is as follows!
  14. [A, B, D]
  15.  
  16.  
  17.  
  18. * */
  19. public static void check(){
  20.  
  21. l.add("A");
  22. l.add("B");
  23. l.add("C");
  24. l.add("D");
  25. Iterator i = l.iterator();
  26. System.out.println("For ith iterator: ");
  27.  
  28. System.out.print(i . next() +" ");
  29. System.out.print(i . next() +" \n");
  30. Iterator j = l.iterator() ;
  31. System.out.println("For jth iterator: ");
  32. System.out.print(j . next() +" ");
  33. System.out.print(j . next() +" ");
  34. System.out.print(j . next() +" ");
  35. //Removes from the underlying collection the last element returned by this iterator
  36. j . remove( ) ;
  37. System.out.println("\n After remove is executed:the list is as follows!");
  38. // now we are removing C, because we are on the element at C
  39. System.out.println(l);
  40.  
  41. }
  42. public static void main(String args[])
  43. {
  44. check();
  45. }
  46.  
  47.  
  48. /*
  49. * For jth iterator:
  50. A B C
  51. For ith iterator:
  52. A B [A, B, D]
  53. */
  54. public static void check2(){
  55.  
  56. l.add("A");
  57. l.add("B");
  58. l.add("C");
  59. l.add("D");
  60.  
  61. Iterator j = l.iterator() ;
  62. System.out.println("For jth iterator: ");
  63.  
  64. System.out.print(j . next() +" ");
  65. System.out.print(j . next() +" ");
  66. System.out.print(j . next() +" \n");
  67. j . remove( ) ;
  68. Iterator i = l.iterator();
  69. System.out.println("For ith iterator: ");
  70. System.out.print(i . next() +" ");
  71. System.out.print(i . next() +" ");
  72. //Removes from the underlying collection the last element returned by this iterator
  73.  
  74. //System.out.println("\n After remove is executed:the list is as follows!");
  75. // now we are removing C, because we are on the element at C
  76. System.out.println(l);
  77.  
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement