生成一个随机字符串

下面代码给出了如何生成一个随机字符串:

 private static String chars= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  private static char[] src = chars.toCharArray();
  public static class NextRandChar {
    public char next() {
      return src[r.nextInt(src.length)];
    }
  }
然后:
public static class RandString{
    private int len;
    private NextRandChar nc = new NextRandChar();
    public RandString(int length) {
      len = length;
    }
    public Object next() {
      char[] buf = new char[len];
      for(int i = 0; i < len; i++)
        buf[i] = nc.next();
      return new String(buf);
    }
  }

你可能感兴趣的:(java点点滴滴,string,object,class)