并且利用他们可以打印任意类型的数组以不同的值或对象填充数组;

解决方案 »

  1.   

    JAVA学习新手,求知心切,望各位大侠帮忙解决!
      

  2.   

    Generator  你要什么效果
      

  3.   

    1.生成对象(生成代码)
    2.利用他们可以打印任意类型的数组
    3.对象填充数组(fill)生成器 接口
    public interface Generator( Object next() )
      

  4.   

    package cn.util;import java.util.*;
    public class Array2 {
             //打印任意类型的数组.
    public static String toString(Object[] a){
    StringBuffer result = new StringBuffer("[");
    for(int i = 0;i < a.length;i++){
    result.append(a[i]);
    if(i < (a.length - 1))
       result.append(",");
    }
    result.append("]");
    return result.toString();
    }
    public static String toString(boolean[] a){
    StringBuffer result = new StringBuffer("[");
    for(int i = 0;i < a.length;i++) {
    result.append(a[i]);
    if(i < (a.length - 1))
       result.append(",") ;
    }
    result.append("]");
    return result.toString();
    }

    public static String toString(byte[] a){
    StringBuffer result = new StringBuffer("[");
    for(int i = 0;i < a.length;i++){
    result.append(a[i]);
    if(i < (a.length-1))
        result.append(",");
    }
    result.append("]");
    return result.toString();
    }

    public static String toString(char[] a) {
    StringBuffer result = new StringBuffer("[");
    for(int i = 0;i < a.length;i++){
    result.append(a[i]);
    if(i < (a.length - 1))
       result.append(",");
    }
    result.append("]");
    return result.toString();
    }

    public static String toString(short[] a){
    StringBuffer result = new StringBuffer("[");
    for(int i = 0;i < a.length;i++) {
    result.append(a[i]);
    if(i < (a.length - 1))
       result.append(",");
    }
    result.append("]");
    return result.toString();
    }

    public static String toString(int[] a) {
    StringBuffer result = new StringBuffer("[");
    for(int i = 0;i < a.length;i++) {
    result.append(a[i]);
    if(i < (a.length - 1))
       result.append(",");
    }
    result.append("]");
    return result.toString();
    }

    public static String toString(long[] a) {
        StringBuffer result = new StringBuffer("[");
    for(int i = 0;i < a.length;i++) {
    result.append(a[i]);
    if(i < (a.length - 1))
       result.append(",");
    }
    result.append("]");
    return result.toString();
    }

    public static String toString(float[] a) {
    StringBuffer result = new StringBuffer("[");
    for(int i = 0;i < a.length;i++) {
    result.append(a[i]);
    if(i < (a.length - 1))
       result.append(",");
    }
    result.append("]");
    return result.toString();
    }

    public static String toString(double[] a) {
    StringBuffer result = new StringBuffer("[");
    for(int i = 0;i < a.length;i++) {
    result.append(a[i]);
    if(i < (a.length - 1))
       result.append(",");
    }
    result.append("]");
    return result.toString();
    }

    public static void fill(Object[] a,Generator g) {
    fill(a, 0, a.length,g);
    }

    public static void fill(Object[] a,int from,int to,Generator g) {
    for(int i = from;i < to;i++) {
    a[i] = g.next();
    }
    }

    public static void fill(boolean[] a,BooleanGenerator g) {
    fill(a ,0, a.length, g);
    }

    public static void fill(boolean[] a,int from,int to ,BooleanGenerator g){
    for(int i = from;i < to;i++)
       a[i] = g.next();
    }

    public static void fill(byte[] a,ByteGenerator g){
    fill(a, 0, a.length, g);
    }

    public static void fill(byte[] a,int from,int to,ByteGenerator g){
    for(int i = from;i < to;i++) {
    a[i] = g.next();
    }
    }

    public static void fill(char[] a,CharGenerator g){
    fill(a, 0, a.length, g);
    }

    public static void fill(char[] a,int from,int to,CharGenerator g){
    for(int i = from;i < to;i++){
    a[i] = g.next();
    }
    }

    public static void fill(short[] a,ShortGenerator g){
    fill(a, 0, a.length, g);
    }

    public static void fill(short[] a,int from,int to,ShortGenerator g){
    for(int i = from;i < to;i++){
    a[i] = g.next();
    }
    }

    public static void fill(int[] a,IntGenerator g){
    fill(a, 0, a.length, g);
    }

    public static void fill(int[] a,int from,int to,IntGenerator g){
    for(int i = from;i < to;i++){
    a[i] = g.next();
    }
    }

    public static void fill(long[] a,LongGenerator g){
    fill(a, 0, a.length, g);
    }

    public static void fill(long[] a,int from,int to ,LongGenerator g){
    for(int i = from;i < to;i++){
    a[i] = g.next();
    }
    }

    public static void fill(float[] a,FloatGenerator g) {
    fill(a, 0, a.length, g);
    }

    public static void fill(float[] a,int from,int to,FloatGenerator g){
    for(int i = from;i < to;i++){
    a[i] = g.next();
    }
    }

    public static void fill(double[] a,DoubleGenerator g){
    fill(a, 0, a.length, g);
    }

    public static void fill(double[] a,int from,int to,DoubleGenerator g){
    for(int i = from;i < to;i++){
    a[i] = g.next();
    }
    }

    public static Random r = new Random();
    public static class RandBooleanGenerator implements BooleanGenerator{
    public boolean next() {
    return r.nextBoolean();
    }
    }

    public static class RandByteGenerator implements ByteGenerator {
    public byte next(){
    return (byte)r.nextInt();
    }
    }

    public static String source = "ACBDDEFADFGFHTYJDXGAFDAVCadfjafjiwejfakjdfa";
    public static char[] src = source.toCharArray();
    public static class RandCharGenerator implements CharGenerator{
    public char next(){
    return src[r.nextInt(src.length)];
    }
    }

    public static class RandStringGenerator implements Generator{
    private RandCharGenerator cg = new RandCharGenerator();
    private int len;
    public RandStringGenerator(int l){
    len = l;
    }
    public Object next() {
    char[] buf = new char[len];
    for(int i = 0;i < len;i++){
    buf[i] = cg.next();
    }
    return new String(buf);
    }
    }

    public static class RandShortGenerator implements ShortGenerator{
    public short next(){
    return (short)r.nextInt();
    }
    }

    public static class RandIntGenerator implements IntGenerator{
    private int seed = 10000;
    public RandIntGenerator(){
    }
    public RandIntGenerator(int seed){
    this.seed = seed;
    }
    public int next(){
    return r.nextInt(seed);
    }
    }

    public static class RandLongGenerator implements LongGenerator{
    public long next(){
    return r.nextLong();
    }
    }

    public static class RandFloatGenerator implements FloatGenerator{
    public float next(){
    return r.nextFloat();
    }
    }

    public static class RandDoubleGenerator implements DoubleGenerator {
    public double next(){
    return r.nextDouble();
    }
    }
    }interface IntGenerator{
              int next();
    }
    interface LongGenerator{
              long next();
    }
    ...其他接口省.
      

  5.   

    楼上的是《Thinking in JAVA》里面的代码吧?怎么全抄过来啦?
      

  6.   

    我主要是问Generator 类怎么生成的?