fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. List list2 = Arrays.asList("1", "2", "3", "4", "5","6","7","8","9","10");
  13. List list3 = Arrays.asList("1", "2", "3", "4", "5","6","7","8","9","10", "11");
  14.  
  15.  
  16.  
  17. arraysList(list2);
  18. linkedList(list2);
  19.  
  20. arraysList(list3);
  21. linkedList(list3);
  22. }
  23.  
  24. private static void linkedList(List list){
  25. LinkedList<String> in = new LinkedList<String>(list);
  26. LinkedList<String> out = new LinkedList<String>();
  27.  
  28. out.addFirst(in.getFirst());
  29.  
  30. Iterator<String> it = in.iterator();
  31. it.next();
  32.  
  33. Iterator<String> rit = in.descendingIterator();
  34. rit.next();
  35.  
  36. int median = in.size() / 2 - 1;
  37.  
  38. for (int i = 0; i < median; i++) {
  39. out.add(rit.next());
  40. out.add(it.next());
  41. }
  42.  
  43. if (in.size() % 2 == 1) {
  44. out.addLast(it.next());
  45. }
  46.  
  47. out.addLast(in.getLast());
  48. System.out.println("LinkedList: "+out + ".size("+out.size()+")");
  49. }
  50.  
  51. private static void arraysList(List list) {
  52. ArrayList<String> in = new ArrayList<String>(list);
  53. ArrayList<String> out = new ArrayList<String>(in.size());
  54.  
  55. out.add(in.get(0));
  56. int last = in.size() - 1;
  57.  
  58. int median = in.size() / 2 ;
  59. for (int i = 1; i < median ; i++) {
  60. String r = in.get(in.size() - i - 1);
  61. String l = in.get(i);
  62. out.add(r);
  63. out.add(l);
  64. }
  65.  
  66. if (in.size() % 2 == 1) {
  67. out.add(in.size() - 2, in.get(median));
  68. }
  69. out.add(last, in.get(last));
  70.  
  71. System.out.println("ArrayList : "+out + ".size("+out.size()+")");
  72. }
  73.  
  74. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
ArrayList : [1, 9, 2, 8, 3, 7, 4, 6, 5, 10].size(10)
LinkedList: [1, 9, 2, 8, 3, 7, 4, 6, 5, 10].size(10)
ArrayList : [1, 10, 2, 9, 3, 8, 4, 7, 5, 6, 11].size(11)
LinkedList: [1, 10, 2, 9, 3, 8, 4, 7, 5, 6, 11].size(11)