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 {
22 a[i] = generator.nextInt(n);
23 }
24
25 return a;
26 }
27
28 /**
29 Swaps two entries of an array.
30 @param a the array
31 @param i the first position to swap
32 @param j the second position to swap
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