第一种数组添加方式以及输出
import java.util.ArrayList;
import java.util.Random;
public class randomTest {
public static void main(String[] args) {
ArrayList<char[]> List3 = new ArrayList<>();
char[] code = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z' };
Random rr = new Random();
for (int j = 0; j < 10; j++) {
//每次上层循环开始时新建数组
char[] code2 = new char[6];
for (int i = 0; i < 6; i++) {
//将6个code数组中的值添加到code2数组中
code2[i] = code[rr.nextInt(61)];
}
//将code2插入到List3集合中
List3.add(code2);
}
for (int i = 0; i < 10; i++) {
//这里不能直接使用System.out.println(List3);否则会输出存储在集合中的数组地址;
System.out.println(List3.get(i));
}
}
}
第二种数组添加方式以及输出
import java.util.ArrayList;
import java.util.Random;
public class Work1 {
public static void main(String[] args) {
//准备验证码数据
ArrayList<Character> datas = new ArrayList<>();
for(char i = 'a',k='A';i<='z';i++,k++) {
datas.add(i);
datas.add(k);
}
for(char i ='0';i<='9';i++) {
datas.add(i);
}
Random r = new Random();
//生产十组验证码
for (int i = 0; i < 10; i++) {
//生产六位
System.out.print("验证码:");
for(int k = 0;k<6;k++) {
Character c = datas.get(r.nextInt(datas.size()));
System.out.print(c);
}
System.out.println();
}
}
}