ArrayUtil.java.html

  1  import java.util.Random;
  2  
  3  /**
  4     This class contains utility methods for array manipulation.
  5  */  
  6  public class ArrayUtil
  7  { 
  8     private static Random generator = new Random();
  9  
 10     /**
 11        Creates an array filled with random values.
 12        @param length the length of the array
 13        @param n the number of possible random values
 14        @return an array filled with length numbers between
 15        0 and n - 1
 16     */
 17     public static int[] randomIntArray(int length, int n)
 18     {  
 19        int[] a = new int[length];      
 20        for (int i = 0; i < a.length; i++)
 21           a[i] = generator.nextInt(n);
 22        
 23        return a;
 24     }
 25  
 26     /**
 27        Swaps two entries of an array.
 28        @param a the array
 29        @param i the first position to swap
 30        @param j the second position to swap
 31     */
 32     public static void swap(int[] a, int i, int j)
 33     {
 34        int temp = a[i];
 35        a[i] = a[j];
 36        a[j] = temp;
 37     }
 38  }
 39