Java - toString 不能正确打印数组

Java - toString doesn't print array correctly

Java 问题。

我想用 to String 方法打印出一个数组, 但我得到的只是:

fifo.Fifo@2a139a55
fifo.Fifo@15db9742

我知道这是指向保存数组的点, 但是我如何使用 toStinr 方法实际打印出数组?

import java.util.ArrayList;

public class Fifo {

    private ArrayList <Object> list;

    public Fifo() {
        this.list = new ArrayList <Object>();
    }

    public void push(Object obj) {
        list.add(obj);
    }

    public ArrayList getList() {
        return this.list;
    }

    public Object pull() {
        if (list.isEmpty()) {
            System.out.println("leer");
            return null;
        }
        Object o = list.get(0);
        list.remove(0);
        return o;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this)
            return true;
        if (o.getClass() == this.getClass()) {
            Fifo other = (Fifo) o;
            ArrayList otherList = other.getList();

            if (otherList.size() == this.getList().size()) {
                boolean sameObjects = true;
                for (int i = 0; i < list.size(); i++) {
                    if (!list.get(i).equals(otherList.get(i))) {
                        sameObjects = false;
                    }
                }
                if (sameObjects)
                    return true;
            }
        }
        return false;
    }

    public Fifo clone() {
        Fifo cloneObj = new Fifo();

        for (int i = 0; i < this.list.size(); i++) {
            cloneObj.push(this.list.get(i));
        }
        return cloneObj;
    }
}

这是单独的测试方法:

import java.util.*;

public class Aufgabe {

    public static void main(String [] args){
        Fifo test = new Fifo();
        Fifo test2;

        test.push(1234);
        test.push("Hallo");
        test.push(5678);
        test.push("du da");

        test.pull();

        System.out.println(test.toString());
        test2=test.clone();
        System.out.println(test2.toString());
        System.out.println(test2.equals(test));
    }
}

Java 中的数组是不覆盖 toString() 方法的对象,因此,正如您所指出的,您将获得包含 class 名称和[默认] hashCode()。要 "properly" 将数组转换为字符串,您可以使用 Arrays.toString:

MyObject[] array = ...;
System.out.println(Arrays.toString(array));

使用这个:

Arrays.toString(yourArray);

toString 将为您打印的数组内容将包含哈希码、数组元素的类型,因此并不是您要查找的内容

覆盖 Object.toString() 并提供可以正常工作的实施。

正如其他人提到的那样,使用 Arrays.toString() 方法。

即将

fifo.Fifo@2a139a55
fifo.Fifo@15db9742

这就是对象 class 中定义的 toString() 方法的工作原理。它打印以下格式的字符串:

ClassName@HexadecimalOfHashCode

这是对象 class 中定义的默认实现。由于您的 FIFO class 不会覆盖 toString() 方法,因此在您的 FIFO 对象上调用 toString() 会调用对象 class.[=16= 中的 toString() 方法]

您需要在 Fifo class 中覆盖 toString() 以打印有用的内容。也许像这样的东西是合适的:

@Override
public String toString() {
    return "Fifo" + list.toString();
}

(这会给你类似 Fifo[element1, element2] 的东西。)

首先,您没有使用数组,您使用的是 ArrayList - 这是 Java.

List 的实现

其次,您不是在打印数组列表,而是在打印包含它的对象 - Foo.

的一个实例
Fifo test = new Fifo();
// do stuff
System.out.println(test.toString());

如果只是想打印出列表,需要使用

Fifo test = new Fifo();
// do stuff
System.out.println(test.getList());

更好的解决方案是在您的 Foo class.

中覆盖 toString
public class Foo {
  // everything you already have

  public String toString() {
    // you can format this however you want
    return "Contents of my list: " + list;
  }
}

当您将对象传递给 System.out.println 时,将自动调用此方法。

Foo test = new Foo();
// do stuff
System.out.println(test);

将导致 Contents of my list: [a, b, c](其中 a, b, c 实际上是您列表中的任何内容)。

补充信息 当您在 Java 中使用 System.out.println 时,请记住:

  • 基元将按原样打印,例如System.out.println(1) 将打印 1System.out.println(false) 将打印 false.
  • 对象实例(即非基元)将具有 println 调用的 toString() 方法。
  • 对象的默认 toStringclassName@hashCode
  • 一维数组默认toString[LclassName@hashCode。额外的维度将在字符串的开头产生额外的 [