【问题标题】:Java Arrays.toString() creates a strange sequence of charactersJava Arrays.toString() 创建一个奇怪的字符序列
【发布时间】:2021-11-27 00:02:28
【问题描述】:

我是 Java 新手,但我在 JavaScript 方面拥有丰富的经验。我有一个包含 15 个随机字符的生成数组,我想将其转换为字符串(例如,数组 { 0、2、f、B、d、7、A} 被转换为 02fBd7A),但它创建了这个奇怪的字符串人物。这就像它实际上取数组并将语法直接放入字符串中(例如数组 { 0, 2, f, B, d, 7, A},而不是“02fBd7A”,而是“[ 0, 2, f , B, d, 7, A ]" 作为字符串。我更精通 JavaScript,而且我知道您可以在 JavaScript 中手动修剪字符串,但是在 Java 中是否有更简单的方法来执行此操作?代码如下(我知道它不能像上面的例子那样生成大写字母,但我认为添加起来很简单)。

import java.util.Arrays;
import java.util.Scanner;

public class passwordGen {

    public static char Number(char[] array) {
        byte random = (byte)(Math.random() * 10);
        return array[random];
    }

    public static char Char(char[] array) {
        byte random = (byte)(Math.random() * 26);
        return array[random];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        final char[] numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        final char[] alphabet = { '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'
        };

        float selector;

        int length = scanner.nextInt();

        char[] passHold = new char[length];
        String passFinal;

        try {
            for (int i = 0; i < length; i++) {
                selector = (float)Math.random();
                if (selector < 0.5) {
                    passHold[i] = Number(numbers);
                } else {
                    passHold[i] = Char(alphabet);
                }
            }

            passFinal = Arrays.toString(passHold);
            System.out.println(passFinal);
        } catch (Exception e) {
            System.out.println("An error occurred.");
        }
    }
}

【问题讨论】:

  • 是的,我知道,但我更多的是在寻找解决方案。我希望它像一个串联的字符串,就像 char 值全部组合成 SOf89f8 一样,而不是数组表示。
  • 这能回答你的问题吗? What's the simplest way to print a Java array?
  • 网站出了点问题,它隐藏了您的大部分评论。现在我读到它会起作用,非常感谢!

标签: java arrays tostring


【解决方案1】:

Arrays.toString 方法创建一个字符串,显示数组中的每个元素。您正在寻找的是 String 构造函数,它接受一个字符数组:

passFinal = new String(passHold);

【讨论】:

    【解决方案2】:

    或者您可以将新选择的字符连接到 passFinal 字符串并跳过中间字符数组。 (你提到你想要在你的一个 cmets 中进行连接。)

    String passFinal = "";
    try {
        for (int i = 0; i < length; i++) {
            selector = (float)Math.random();
            if (selector < 0.5) {
                passFinal += Number(numbers);
            } else {
                passFinal += Char(alphabet);
            }
        }
    
        System.out.println(passFinal);
    }
    

    可能更好的是使用 StringBuilder 及其附加-

    StringBuilder passFinalSb = new StringBuilder();
    try {
        for (int i = 0; i < length; i++) {
            selector = (float)Math.random();
            if (selector < 0.5) {
                passFinalSb.append(Number(numbers));
            } else {
                passFinalSb.append(Char(alphabet));
            }
        }
        String passFinal = passFinalSb.toString();
        System.out.println(passFinal);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2013-10-14
      相关资源
      最近更新 更多