TWiki . Doc . ListsBeginnerExample

/* Please try the NoviceExamples before the BeginnerExamples
To compile:
   nicec --sourcepath=.. -a lists.jar lists
To run:
   java -jar lists.jar 16
*/


void main(String[] args){
   var n = toSingleInt(args);

   let nSize = 10000;
   int L1Count = 0;

   while (n-- > 0){
      // initialize L1
      ArrayList<int> L1 = new ArrayList(nSize);
      for (var j = 1; j <= nSize; j++) L1.add(j);

      // copy L1 to L2
      ArrayList<int> L2 = L1.clone;

      // remove from left of L2 add to right of L3
      ArrayList<int> L3 = new ArrayList(nSize);
      while (L2.size > 0) 
         L3.add( L2.removeAt(0) ); 

      // remove from right of L3 add to right of L2
      int index;
      while ( (index = L3.size) > 0) 
         L2.add( L3.removeAt(index - 1) );

      // reverse L1
      Collections.reverse(L1);

      // check that first item is now SIZE
      // NOTE: no Object to int type cast needed

      if (L1[0] != nSize) println("First item of L1 != SIZE");

      // check that L1 == L2
      if ( !L1.equals(L2) ) println("L1 != L2");
      L1Count = L1.size;
      }

   println(L1Count);
}


int toSingleInt(String[] s){
   try { return Integer.parseInt(s[0]); }
   catch (Exception e){ return 1; } }


/* Notes - language
Compare with Java at the "Win32 Language Shootout"

*/</verbatim>

-- Main.IsaacGouy - 27 Aug 2003

----- Revision r1.5 - 10 Jan 2004 - 19:05 GMT - IsaacGouy
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.