Java数组(3):创建测试数据

有时我们需要使用数组批量创建测试数据,接下来通过以下4点来举例。

(1) 使用Arrays.fill()填充数据

(2) 使用Random类中JDK1.8提供的新方法用来生成随机数

(3) 一个随机数生成器的实例

(4) 根据(3)中的随机数生成器创建包装类型的数组及通过转换器转换成基本类型的数组

 

(1) 使用Arrays.fill()填充数据:它可以填充整个数组或填充数组某个区域。另外填充对象时是复制一个引用填充的。

 1 public class Test1 {
 2     public static void main(String[] args) {
 3         char[] p1 = new char[3];
 4         long[] p2 = new long[4];
 5         String[] p3 = new String[5];
 6         Arrays.fill(p1, 'b');
 7         Arrays.fill(p2, 5L);
 8         Arrays.fill(p3, "Hello");
 9         System.out.println(Arrays.toString(p1)); // [b, b, b]
10         System.out.println(Arrays.toString(p2)); // [5, 5, 5, 5]
11         System.out.println(Arrays.toString(p3)); // [Hello, Hello, Hello, Hello, Hello]
12         Arrays.fill(p3, 4, 5, "World");
13         System.out.println(Arrays.toString(p3)); // [Hello, Hello, Hello, Hello, World]
14     }
15 }

(2) 使用Random类中JDK1.8提供的新方法用来生成随机数

 1 public class Test2 {
 2     public static void main(String[] args) {
 3         Random random = new Random();
 4         int[] arr = random.ints(10).toArray(); // 生成10个int范围类的个数。
 5         System.out.println(Arrays.toString(arr));
 6         random.ints().limit(10).forEach(System.out::println); // 生成10个int范围类的个数(先生成无限个再截取10个)。
 7         random.longs(5, 10, 100).forEach(System.out::println); // 生成5个在[10,100)范围内的整数
 8         random.longs(10, 100).limit(5).forEach(System.out::println); // 生成5个在[10,100)范围内的整数(先生成无限个再截取10个)。
 9     }
10 }

(3) 一个随机数生成器的实例:因为Random构造器使用常量初始化,所以,每次使用这些Generator中的一个来运行程序时,所产生的输出都是可重复的。

  1 import java.util.Random;
  2 
  3 interface Generator<T> { T next(); }
  4 
  5 class RandomGenerator {
  6 
  7     // 对于有参构造,需要注意的是,如果seed(种子)值相同,不管执行多少次,随机生成的数据是相同的
  8     private static Random r = new Random(47);
  9 
 10     // Boolean Generator
 11     public static class GenBoolean implements Generator<Boolean> {
 12         @Override
 13         public Boolean next() {
 14             return r.nextBoolean(); // return next(1) != 0; 想生成范围在[0,1]的整数
 15         }
 16     }
 17 
 18     // Byte Generator
 19     public static class GenByte implements Generator<Byte> {
 20         @Override
 21         public Byte next() {
 22             return (byte) r.nextInt(); // 随机生成一个整数,范围就是int类型的范围-2^31~2^31-1,这里会强制截取后8 bit作为byte值
 23         }
 24     }
 25 
 26     // Character Generator
 27     public static class GenCharacter implements Generator<Character> {
 28 
 29         private static char[] chars = ("abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
 30 
 31         @Override
 32         public Character next() {
 33             return chars[r.nextInt(GenCharacter.chars.length)]; // 生成范围在[0, chars.length)的整数
 34         }
 35     }
 36 
 37     // String Generator
 38     public static class GenString implements Generator<String> {
 39         private int length = 7;
 40         Generator<Character> cg = new GenCharacter();
 41 
 42         public GenString() {
 43         }
 44 
 45         public GenString(int length) {
 46             this.length = length;
 47         }
 48 
 49         @Override
 50         public String next() {
 51             char[] buf = new char[length];
 52             for (int i = 0; i < length; i++)
 53                 buf[i] = cg.next();
 54             return new String(buf);
 55         }
 56     }
 57 
 58     // Short Generator
 59     public static class GenShort implements Generator<Short> {
 60 
 61         @Override
 62         public Short next() {
 63             return (short) r.nextInt(); // 随机生成一个整数,范围就是int类型的范围-2^31~2^31-1,这里会强制截取后16 bit作为byte值
 64         }
 65     }
 66 
 67     // Integer Generator
 68     public static class GenInteger implements Generator<Integer> {
 69         private int mod = 10000;
 70 
 71         public GenInteger() {
 72         }
 73 
 74         public GenInteger(int modulo) {
 75             mod = modulo;
 76         }
 77 
 78         @Override
 79         public Integer next() {
 80             return r.nextInt(mod); // 随机生成一个整数,默认范围是[0, 10000)
 81         }
 82     }
 83 
 84     // Long Generator
 85     public static class GenLong implements Generator<Long> {
 86         private int mod = 10000;
 87 
 88         public GenLong() {
 89         }
 90 
 91         public GenLong(int modulo) {
 92             mod = modulo;
 93         }
 94 
 95         @Override
 96         public Long next() {
 97             return new Long(r.nextInt(mod));// 随机生成一个整数,默认范围是[0, 10000)
 98         }
 99     }
100 
101     // Float Generator
102     public static class GenFloat implements Generator<Float> {
103         @Override
104         public Float next() {
105             int trimmed = Math.round(r.nextFloat() * 100);
106             return ((float) trimmed) / 100;
107         }
108     }
109 
110     // Double Generator
111     public static class GenDouble implements Generator<Double> {
112         @Override
113         public Double next() {
114             long trimmed = Math.round(r.nextDouble() * 100);
115             return ((double) trimmed) / 100;
116         }
117     }
118 }
119 
120 public class Test3 {
121     public static void main(String[] args) {
122         Generator<Boolean> a = new RandomGenerator.GenBoolean();
123         Generator<Integer> b = new RandomGenerator.GenInteger();
124         Generator<Float> c = new RandomGenerator.GenFloat();
125         generate(a, 5); // true false true false false
126         generate(b, 5); // 429 4868 200 4522 6207
127         generate(c, 5); // 0.27 0.95 0.26 0.11 0.05
128     }
129 
130     private static <T> void generate(Generator<T> gen, int times) {
131         for (int i = 0; i < times; i++) {
132             System.out.print(gen.next() + " ");
133         }
134         System.out.println();
135     }
136 }

(4) 根据(3)中的随机数生成器创建包装类型的数组及通过转换器转换成基本类型的数组

> 创建包装类型的数组

> 由于泛型不支持基本类型,如果想用生成器填充基本类型的数组,还需要创建一个转换器

  1 import java.lang.reflect.Array;
  2 import java.util.ArrayList;
  3 import java.util.Arrays;
  4 
  5 class CollectionData<T> extends ArrayList<T> {
  6 
  7     private static final long serialVersionUID = 1L;
  8 
  9     public CollectionData(Generator<T> gen, int quantity) {
 10         for (int i = 0; i < quantity; i++)
 11             add(gen.next());
 12     }
 13 
 14     public static <T> CollectionData<T> list(Generator<T> gen, int quantity) {
 15         return new CollectionData<T>(gen, quantity);
 16     }
 17 }
 18 
 19 class Generated {
 20     // Fill an existing array:
 21     public static <T> T[] array(T[] a, Generator<T> gen) {
 22         return new CollectionData<T>(gen, a.length).toArray(a);
 23     }
 24 
 25     // Create a new array:
 26     public static <T> T[] array(Class<T> type, Generator<T> gen, int size) {
 27         @SuppressWarnings("unchecked")
 28         T[] a = (T[]) Array.newInstance(type, size);
 29         return new CollectionData<T>(gen, size).toArray(a);
 30     }
 31 }
 32 
 33 class ConvertTo {
 34     public static boolean[] primitive(Boolean[] in) {
 35         boolean[] result = new boolean[in.length];
 36         for (int i = 0; i < in.length; i++)
 37             result[i] = in[i];
 38         return result;
 39     }
 40 
 41     public static char[] primitive(Character[] in) {
 42         char[] result = new char[in.length];
 43         for (int i = 0; i < in.length; i++)
 44             result[i] = in[i];
 45         return result;
 46     }
 47 
 48     public static byte[] primitive(Byte[] in) {
 49         byte[] result = new byte[in.length];
 50         for (int i = 0; i < in.length; i++)
 51             result[i] = in[i];
 52         return result;
 53     }
 54 
 55     public static short[] primitive(Short[] in) {
 56         short[] result = new short[in.length];
 57         for (int i = 0; i < in.length; i++)
 58             result[i] = in[i];
 59         return result;
 60     }
 61 
 62     public static int[] primitive(Integer[] in) {
 63         int[] result = new int[in.length];
 64         for (int i = 0; i < in.length; i++)
 65             result[i] = in[i];
 66         return result;
 67     }
 68 
 69     public static long[] primitive(Long[] in) {
 70         long[] result = new long[in.length];
 71         for (int i = 0; i < in.length; i++)
 72             result[i] = in[i];
 73         return result;
 74     }
 75 
 76     public static float[] primitive(Float[] in) {
 77         float[] result = new float[in.length];
 78         for (int i = 0; i < in.length; i++)
 79             result[i] = in[i];
 80         return result;
 81     }
 82 
 83     public static double[] primitive(Double[] in) {
 84         double[] result = new double[in.length];
 85         for (int i = 0; i < in.length; i++)
 86             result[i] = in[i];
 87         return result;
 88     }
 89 }
 90 
 91 public class Test4 {
 92     public static void main(String[] args) {
 93         Integer[] a = Generated.array(new Integer[5], new RandomGenerator.GenInteger());
 94         System.out.println(Arrays.toString(a)); // [9258, 555, 6693, 1861, 961]
 95         Long[] b = Generated.array(Long.class, new RandomGenerator.GenLong(), 5);
 96         System.out.println(Arrays.toString(b)); // [429, 4868, 200, 4522, 6207]
 97 
 98         int[] c = ConvertTo.primitive(a);
 99         System.out.println(Arrays.toString(c)); // [9258, 555, 6693, 1861, 961]
100         long[] d = ConvertTo.primitive(b);
101         System.out.println(Arrays.toString(d)); // [429, 4868, 200, 4522, 6207]
102     }
103 }

 

posted @ 2018-02-01 22:52  Storm_L  阅读(1018)  评论(0编辑  收藏  举报