import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class zifuchuan {
private List<String>string;
//StringBuilder sb = new StringBuilder() ;
//Random random = new Random();不能在此处定义sb和random;
String k;
public zifuchuan(){
this.string = new ArrayList<String>();
}
/**
- 利用charAt()方法来实现在st这个长字符串中选取合适长度的和合适的字母组成随机的字符串;
- 利用循环;
*利用apend函数进行字符串的插入; - 因为charAt方法在括号中要加入索引位置,故在其方法括号中利用随机数来进行选择;
-
@return
*/
public String creat(){
StringBuilder sb = new StringBuilder() ;
Random random = new Random();
String st = "01234ABCmopqDEFGnKNO579PQRSTUVW6XYZabcdHIJefghijklrstLMwx8yz";
int sr = random.nextInt(10)+1;
for(int i = 0;i < sr;i++){
sb.append( st.charAt(random.nextInt(st.length())));
}
return sb.toString();
}
public void test(){
for(int j = 0;j<10;j++){
do{
k = creat();
}while(string.contains(k));
string.add(k); //*此处利用函数调用**
}
}
public void foreach(){
System.out.println("---------------排序前---------");
for (String string2 : string) {
System.out.println(string2);
}
}
public void testforeach(){
System.out.println("---------------排序后---------");
Collections.sort(string);
for (String string2 : string) {
System.out.println(string2);
}
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
zifuchuan other = (zifuchuan) obj;
if (k == null) {
if (other.k != null)
return false;
} else if (!k.equals(other.k))
return false;
return true;
}public static void main(String []args){
zifuchuan zf = new zifuchuan();zf.test(); zf.foreach(); zf.testforeach();
}
}