01: import java.util.Random;
02: 
03: /**
04:    This class contains utility methods for array 
05:    manipulation.
06: */
07:    
08: public class ArrayUtil
09: {  
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:       Random generator = new Random();
21:       
22:       for (int i = 0; i < a.length; i++)
23:          a[i] = generator.nextInt(n);
24:       
25:       return a;
26:    }
27: 
28:    /**
29:       Swaps two elements in an array.
30:       @param a the array with the elements to swap
31:       @param i the index of one of the elements
32:       @param j the index of the other element
33:    */
34:    public static void swap(int[] a, int i, int j)
35:    {  
36:       int temp = a[i];
37:       a[i] = a[j];
38:       a[j] = temp;
39:    }
40:    
41:    /** 
42:       Prints all elements in an array.
43:       @param a the array to print
44:    */
45:    public static void print(int[] a)
46:    {  
47:       for (int e : a)
48:          System.out.print(e + " ");
49:       System.out.println();
50:    }
51: }
52: