简体   繁体   English

如何在 Java 中覆盖 ArrayList 的 toString 方法?

[英]How can I override the toString method of an ArrayList in Java?

I would like to have my own implementation of the toString() method for an ArrayList in Java.我想在 Java 中为 ArrayList 实现我自己的 toString() 方法。 However, I can't get it working even though I added my toString() like this to the class that contains the ArrayList.但是,即使我像这样将 toString() 添加到包含 ArrayList 的类中,我也无法让它工作。

@Override
public String toString() {
    String result = "+";
    for (int i = 0; i < list.size(); i++) {
        result += " " + list.get(i);
    }
    return result;
}

When I call my ArrayList like this list.toString() , I still get the default representation.当我像这样调用我的 ArrayList list.toString() ,我仍然得到默认表示。 Am I missing something?我错过了什么吗?

ArrayList<String> list = new ArrayList<String>()
{
    private static final long serialVersionUID = 1L;

    @Override public String toString()
    {
        return super.toString();
    }
};

You can't override the toString method of ArrayList 1 .您不能覆盖ArrayList 1toString方法。 Instead, you can use a utility class that converts an ArrayList to a String the way you want/need.相反,您可以使用实用程序类以您想要/需要的方式将ArrayList转换为String Another alternative would be using Arrays.deepToString(yourList.toArray()) .另一种选择是使用Arrays.deepToString(yourList.toArray())

Using Java 8, this can be done very easily:使用 Java 8,这可以很容易地完成:

//List<String> list
System.out.println(String.join(",", list));

Or if you have a List<Whatever> :或者,如果您有一个List<Whatever>

System.out.println(
    list.stream()
        .map(Whatever::getFieldX())
        .collect(Collectors.joining(", "))
);

I'm still against extending ArrayList or similar, is technically possible but I don't see it as a good option.我仍然反对扩展ArrayList或类似的东西,在技术上是可行的,但我认为它不是一个好的选择。


1 you could extend ArrayList and override the toString but generally that's not a great idea. 1您可以扩展ArrayList并覆盖toString但通常这不是一个好主意。 Further info:更多信息:

You need to write a class extending from ArrayList:您需要编写一个从 ArrayList 扩展的类:

import java.util.ArrayList;

public class MyArrayList extends ArrayList {
    private static final long serialVersionUID = 1L;

    @Override
    public String toString() {
        String result = "+";
        for (int i = 0; i < this.size(); i++) {
            result += " " + this.get(i);
        }
        return result;
    }
}

You should do something like你应该做类似的事情

public static String listToString(List<?> list) {
    String result = "+";
    for (int i = 0; i < list.size(); i++) {
        result += " " + list.get(i);
    }
    return result;
}

and pass the list in as an argument of listToString() .并将列表作为listToString()的参数listToString() You can technically extend ArrayList (either with an anonymous class or a concrete one) and implement toString yourself, but that seems unnecessary here.可以在技术上扩展ArrayList (使用匿名类或具体类)并自己实现toString ,但这在这里似乎没有必要。

To override toString() for ArrayList Integer array:要覆盖 ArrayList 整数数组的 toString():

class MyArrayList extends ArrayList<Integer> {
    private static final long serialVersionUID = 1L;
    @Override public String toString(  ) {
        String output = "";
        for ( int i = 0 ; i < this.size(); i++ )
            output = Integer.toString( this.get( i ) );
        return output;
        }
    }

ArrayList<Integer> list = 
    new ArrayList<>(Arrays.asList( 5, 4, 2, 9, 1, 7 ) );
System.out.print( "list = " + list );

will generate: list = [5, 4, 2, 9, 1, 7]将生成:list = [5, 4, 2, 9, 1, 7]

You can override the toString method in a new created class but you should make sure the created class extends ArrayList.您可以在新创建的类中覆盖 toString 方法,但应确保创建的类扩展了 ArrayList。 Then, you can override toString method.然后,您可以覆盖 toString 方法。 This is based on IS relationship.这是基于IS关系。 The new created class is a type of ArrayList.新创建的类是一种 ArrayList。

You can also create an ArrayList, which is based on the HAS relationship.您还可以创建一个 ArrayList,它基于 HAS 关系。 In this case, you have to write another method to return String instead of override toString method.在这种情况下,您必须编写另一个方法来返回 String 而不是覆盖 toString 方法。

I would like to have my own implementation of the toString() method for an ArrayList in Java.我想为Java中的ArrayList拥有toString()方法的自己的实现。 However, I can't get it working even though I added my toString() like this to the class that contains the ArrayList.但是,即使我将这样的toString()添加到包含ArrayList的类中,我也无法正常工作。

@Override
public String toString() {
    String result = "+";
    for (int i = 0; i < list.size(); i++) {
        result += " " + list.get(i);
    }
    return result;
}

When I call my ArrayList like this list.toString() , I still get the default representation.当我像这样的list.toString()调用ArrayList时,我仍然得到默认表示。 Am I missing something?我想念什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM