如何使用来自不同阵列的随机选择的字符填充数组

时间:2013-10-23 18:44:49

标签: java arrays

顺便说一句,这是在java中完成的。

所以我试图用字符创建某种模式生成器。我目前的代码将创建一个数组,它将从输入的第一个数字开始无限打印,直到最后一个数字,从第一个数组中获取字符。错误是,如果我从接近结束开始并告诉它复制超过那个; (比如从12个字符开始,再复制15个字符,但原始数组中只有14个字符。)如何在原始数组结束后继续填充数组,比如从头开始并继续运行通过阵列直到它填满。\

import java.util.*;
public class patternGenerator  {
    private static Scanner input;
    public static void main(String[] args)   {      
    char[] anArray = {'!','@','#','$','%','^','&','*','+','=','~','<','>','?'}; 
    int symbolsPerLine = 0, printed, dontStop = 1, difSymbols;
    input = new Scanner(System.in);
    System.out.println("Enter the amount of symbols to use: ");
    difSymbols = input.nextInt();
    while (difSymbols > anArray.length) {
        difSymbols = (anArray.length-1);
    }
    System.out.println("Dif symbols are: "+ difSymbols);
    System.out.println("Enter amount of symbols per row: ");
    symbolsPerLine = input.nextInt();
    System.out.println("Amount of symbols per line are: "+ symbolsPerLine);
    char [] patternArray = new char[symbolsPerLine];
    System.arraycopy(anArray, difSymbols, patternArray, 0, symbolsPerLine);
    System.out.print(patternArray);
    while(dontStop == 1) {
            System.out.print("\n");
            printed = 0;
            for (int a = 0; a< patternArray.length; a++) {
                    System.out.print(patternArray[a]);
                }
                printed++;
        }
    }
}

4 个答案:

答案 0 :(得分:1)

可以使用Apache的RandomStringUtils.html#random(int, char...)中的commons-lang执行此操作。我不知道JDK中的单一方法解决方案。

答案 1 :(得分:1)

所以我注意到的第一件事就是你有一个无限循环。你留下dontStop allways == 1所以你的代码永远不会退出while循环。第二个System.arraycopy(anArray,difSymbols,patternArray,0,symbolsPerLine); 当您使用大于anArray.length的数字时,将得到一个超出范围的错误,因此您需要删除此代码。这是一个建议的解决方案:

        char [] patternArray = new char[symbolsPerLine];

        int length = symbolsPerLine;

        int spot = 0;



        if(symbolsPerLine<anArray.length){
            symbolsPerLine = anArray.length;
        }


        while(dontStop==1){

            for(int i= (anArray.length - difSymbols);i<anArray.length-1;i++){

                if(length==0){
                    dontStop =0;
                    break;
                }


                patternArray [spot]= anArray [i];
                spot++;
                length--;
            }

        }

        for(int j=0;j<patternArray.length;j++){
            System.out.print(patternArray[j]);
        }

        System.out.println("\npatternArray.length is: "+ patternArray.length);

此解决方案的输出是: 输出Run1: 13 差符号是:13 输入每行的符号数量: 20 每行的符号数量为:20 完成循环 @#$%^&安培; + =〜&LT;&GT; @#$%^&安培; + patternArray.length是:20

输出Run2: 输入要使用的符号数量: 五 差符号是:5 输入每行的符号数量: 20 每行的符号数量为:20 完成循环 =〜&LT;&GT; =〜&LT;&GT; =〜&LT;&GT; =〜&LT;&GT; =〜&LT;&GT; patternArray.length是:20

这就是我假设你要完成的事情。

答案 2 :(得分:1)

您可以尝试在源数组上添加索引,并在填充模式数组时循环它:

import java.util.*;
public class patternGenerator  {
    private static Scanner input;
    public static void main(String[] args)   {      
    char[] anArray = {'!','@','#','$','%','^','&','*','+','=','~','<','>','?'}; 
    int symbolsPerLine = 0, printed, dontStop = 1, difSymbols;
    input = new Scanner(System.in);
    System.out.println("Enter the amount of symbols to use: ");
    difSymbols = input.nextInt();
    while (difSymbols > anArray.length) {
        difSymbols = (anArray.length-1);
    }
    System.out.println("Dif symbols are: "+ difSymbols);
    System.out.println("Enter amount of symbols per row: ");


    symbolsPerLine = input.nextInt();

    System.out.println("Amount of symbols per line are: "+ symbolsPerLine);
    char [] patternArray = new char[symbolsPerLine];

    int srcIndex =0;
    for(j=0; j < symbolsPerLine; j++) {
       patternArray[j] = anArray[srcIndex];
      srcIndex++;
      if(srcIndex> (anArray.length-1)) srcIndex =0;

    }

    System.out.print(patternArray);

}

答案 3 :(得分:1)

您可以使用以下内容。我重新考虑了你的代码。

我使用CharBuffer,因为它有一些很好的未来,比如Buffer.hasRemaining()(仍然是缓冲区中的某个位置)和Buffer.put(...)(复制到缓冲区中)可以派上用场当遇到这样的问题时。

在您的问题标题中,您想要随机选择字符,但在您的解决方案中,您只需将数组附加到其上即可。我使用SecureRandom.nextIn(...)添加了一些可能的随机性解决方案。

import java.nio.CharBuffer;
import java.security.SecureRandom;
import java.util.Scanner;

public class PatternGenerator {


  public static void main(String[] args) {
    char[] anArray = {'!', '@', '#', '$', '%', '^', '&', '*', '+', '=', '~', '<', '>', '?'};
    int symbolsPerLine = 0, difSymbols = 0;

    Scanner input = new Scanner(System.in);

    System.out.println("Enter the amount of symbols to use: ");
    difSymbols = input.nextInt();
    difSymbols = difSymbols < anArray.length ? difSymbols : anArray.length - 1;
    System.out.println("Dif symbols are: " + difSymbols);

    System.out.println("Enter amount of symbols per row: ");
    symbolsPerLine = input.nextInt();
    System.out.println("Amount of symbols per line are: " + symbolsPerLine);
    CharBuffer cb = CharBuffer.allocate(symbolsPerLine);

    /*
     * just a copy of the original array appended.
     */

    int offset = 0;
    int length = 0;
    while (cb.hasRemaining()) {
      length = (cb.remaining() > difSymbols ? difSymbols : cb.remaining()) - offset;
      cb.put(anArray, offset, length);
      offset = (offset + length) % difSymbols;
    }
    // prepare the buffer to be read
    cb.flip();
    System.out.println("appended: " + cb);

    /*
     * use random characters now
     */
    cb.clear();
    SecureRandom secureRandom = new SecureRandom();
    while (cb.hasRemaining())
      cb.put(anArray[secureRandom.nextInt(anArray.length)]);
    cb.flip();

    System.out.println("random: " + cb);


    input.close();
  }
}