java - 在 Java 中使用 toString() 打印数组

标签 java arrays string tostring

我正在尝试打印出不带所有括号和逗号的数组,因此我尝试重写 toString() 方法;但是我得到的输出是 [I@5c647e05.抱歉,如果我的帖子格式不好,这是我在这里的第一篇帖子。我试图将数组显示为二进制数。我尝试过 toString() 但它包含括号和逗号,我不能拥有它。

这是我的代码。

import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
import java.util.*;

public class halfAdder {
    public static int[] binary = new int[2];

    public static void main(String[] args) {
        // Declaring booleans for the adder
        int sum = 0;
        int carry = 0;
        boolean a = false;
        boolean b = false;
        int tempA = 0;
        int tempB = 0;
        int notA = 0;
        int notB = 0;


        // Collecting all the information needed from the user
        Scanner console = new Scanner(System.in);

        System.out.print("Please enter your input for A: ");
        tempA = console.nextInt();
        System.out.print("Please enter an input for B: ");
        tempB = console.nextInt();


        // Deciding if what we collected as an input is either 0 or 1
        if(tempA == 0)
        {
            a = false;
            notA = 1;
            System.out.println("A hit first");
        }
        else
        {
            System.out.println("hit second");
            a = true;
            notA = 0;
        }

        if(tempB == 0)
        {
            System.out.println("B hit first");
            b = false;
            notB = 1;
        }
        else
        {
            System.out.println("B hit second");
            b = true;
            notB = 0;
        }

        sum = (notA*tempB)+(tempA*notB);

         if(tempA == 1 && tempB == 1)
        {
            carry = 1;
            sum = 0;
        } 

        binary[0] = carry;
        binary[1] = sum;


        System.out.println("a = " + tempA);
        System.out.println("b = " + tempB);
        System.out.println("not a = " + notA);
        System.out.println("not b = " + notB);
        System.out.println("Sum = " + sum);
        System.out.println("Carry = " + carry);
        System.out.println(binary.toString());
    }

    public String toString()
    {
        String binaryNumber = "The binary number is: ";

        for(int i = 0; i < binary.length; i++)
        {
            binaryNumber += binary[i];
        }
        return binaryNumber;
    }
}

最佳答案

如果您希望将数组转换为字符串,请使用Arrays类:

String arrayAsString = Arrays.toString(binary);

有很多方法可以像二进制数一样显示数组的内容(没有上述方法添加的逗号和括号)。如果使用 Arrays.toString,您可以使用 String 类中提供的方法解析返回的 String。或者,您可以只使用 StringBuilder (或字符串添加)并循环遍历数组。

关于java - 在 Java 中使用 toString() 打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759137/

相关文章:

java - 如何在 Apache Beam/Google Dataflow 中使用 ParseJsons?

java - 简单 map 读取程序中的子字符串索引关闭

java - 如何以日历格式打印数组中的值?

javascript - 将字符串按照指定长度分割成数组

java - 下面的代码问题解决方案在本地 IDE 中对我有用,但在 Hackerrank IDE 中以某种方式失败,我的代码是否有任何问题

java - 使 JComboxBox maximumRowCount 适应 JFrame 或屏幕尺寸

java - 具有相同实体的 Fetchtype.LAZY

string - Lua 模式排除

javascript - 获取原型(prototype)之前的字符串值

c++ - 最小唯一字符