title: How do I print from a vector of vectors in java
body: <p>I just started Java Programming. I am doing some questions from the Programming Challenges Book. The question was to print out the maximum cyclelength of a given set of integers. Here is my code:</p>
  
  <pre><code>public class JavaApplication9 {
  
      /**
       * @param args the command line arguments
       */   
  
      public static void main(String[] args) {
          // TODO code application logic here
          System.out.println("This is a program that calculates the maximum"
                  + " cyclelength of two integers between 1 and 1000000");
          Scanner keyboard = new Scanner(System.in);
          System.out.print("Enter the first number: ");
          int x = keyboard.nextInt();
          System.out.println();
          System.out.print("Enter the second number: ");
          int y = keyboard.nextInt();
          @SuppressWarnings("UseOfObsoleteCollectionType")
          Vector&lt;Integer&gt; cycleVec = new Vector&lt;Integer&gt;();
          Vector newVec = new Vector(); 
          if (x &lt;= 0 || y &lt;= 0 || x &gt;= 1000000 || y &gt;= 1000000)
              System.out.println("Wrong input. Input must be greater than 0"
                 + " and less than 1,000,000!!");
          else{
              for(int k = Math.min(x, y);k&lt;=Math.max(x, y);k++){
                     // We have to count 1 also
                     cycleVec.add(1 + cycleLength(k));  
                     newVec.add(numList(k));
              }
              Enumeration vEnum = cycleVec.elements();
              while(vEnum.hasMoreElements()){
                  int a = (int) vEnum.nextElement();
                  display(newVec, x, y, a);
              }
              System.out.println("\nElements in vector of cyclelengths:");
              while(vEnum.hasMoreElements())
                  System.out.print(vEnum.nextElement() + " ");
              System.out.println();
              Object obj = Collections.max(cycleVec);
              System.out.printf("%d %d ", x, y);
              System.out.println(obj);
          }
      }
  
      public static int cycleLength(int x){
          int termPoint = 0;
          int count = 0;
          while (termPoint != 1){
              if (x % 2 == 0){
                  x = x/2;
                  termPoint = x;
              }else{
                  x = x*3 + 1;
                  termPoint = x;
              }
              ++count;
          }
          return count;
      }
  
      public static Vector numList(int x){
          int termPoint = 0;
          Vector vec = new Vector();
          while (termPoint != 1){
              if (x % 2 == 0){
                  x = x/2;
                  termPoint = x;
              }else{
                  x = x*3 + 1;
                  termPoint = x;
              }
              vec.addElement(x);
          }
          return vec;
      }
  
      public static void display(Vector v, int x, int y, int size){
          System.out.println("Elements generated:");
          int m = 0;
          for(int k = Math.min(x, y); k &lt;= Math.max(x, y); k++){
              System.out.print("Number " + k + " ");
              for (int i = 0; i &lt; v.size(); i++){
                  for (int j = 0; j &lt; size; j++){
                      m = (int)((Vector)v.elementAt(i)).elementAt(j);
                      System.out.print(m + " ");
                  }
                  System.out.println();
              }
          }
      }
  }
  </code></pre>
  
  <p>The output is </p>
  
  <pre><code>run:
  This is a program that calculates the maximum cyclelength of two integers between 1 and 1000000
  Enter the first number: 201
  
  Enter the second number: 210
  Elements generated:
  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18 &gt;= 18
      at java.util.Vector.elementAt(Vector.java:474)
  Number 201604 302 151 454 227 682 341 1024 512 256 128 64 32 16 8 4 2 1     at javaapplication9.JavaApplication9.display(JavaApplication9.java:95)
      at javaapplication9.JavaApplication9.main(JavaApplication9.java:44)
  Java Result: 1
  BUILD SUCCESSFUL (total time: 5 seconds)
  </code></pre>
  
  <p>The first part of the code works (if you remove the display function). I just wanted to print out for each integer x, the list of integers generated and the list of cyclelengths and the maximum cyclelength. Also is there a way I can make this code more efficient (reduce the number of lines of code)?</p>
  
  <p>The new code for the display(It works)</p>
  
  <pre><code> public static void display(Vector v){
          System.out.println("Elements generated:");
          int m = 0, w = 0;     
          for (int i = 0; i &lt; v.size(); i++){
              Vector inner = (Vector)v.elementAt(i);
              System.out.println("Number ");
              for (int j = 0; j &lt; inner.size(); j++){
                  m = (int)inner.elementAt(j);
                  if (j == 0){
                      if (m % 2 == 0){
                          w = (m-1)/3;
                      }else{
                          w = m * 2;
                      }
                  System.out.print(w + " ");
                  }
                  System.out.print(m + " ");
              }
              System.out.println();
          }
      }
  </code></pre>