如何打印嵌套的ArrayList?

huangapple go评论52阅读模式
英文:

How to print a nested ArrayList?

问题

I want to print a nested ArrayList in Java as the title suggests and I'm unable to do that because of conflict with another method.

Here's my code:

import java.util.*;
class TBF
{
    <T> void print(ArrayList<T> a)
    {
        for(int i=0;i<a.size();i++)
        System.out.print(a.get(i)+" ");
        System.out.println();
    }

    <T> void print(ArrayList<ArrayList<T>> a) // this line shows the error: "Erasure of method print(ArrayList<ArrayList<T>>) is the same as another method in type TBF"
    {
        for(int i=0;i<a.size();i++)
        print(a.get(i));
    }

    public static void main(String args[])
    {
        // some code
    }
}

This is unlike any error I received while writing the equivalent code in C++ with vector.

So I then tried this:

import java.util.*;
class TBF
{
    <T> void print(ArrayList<T> a)
    {
        for(int i=0;i<a.size();i++)
        if(a.get(i) instanceof Integer || a.get(i) instanceof Long || a.get(i) instanceof String)
        System.out.print(a.get(i)+" ");
        else
        print(a.get(i)); // this time the error is: "The method print(ArrayList<T>) in the type TBF is not applicable for the arguments (T)"
        System.out.println();
    }

    public static void main(String args[])
    {
        // some code
    }
}

Now I can't understand how I'm supposed to print a 2D ArrayList. Please help.

英文:

I want to print a nested ArrayList in Java as the title suggests and I'm unable to do that because of conflict with another method.

Here's my code:

import java.util.*;
class TBF
{
    &lt;T&gt; void print(ArrayList&lt;T&gt; a)
    {
        for(int i=0;i&lt;a.size();i++)
        System.out.print(a.get(i)+&quot; &quot;);
        System.out.println();
    }

    &lt;T&gt; void print(ArrayList&lt;ArrayList&lt;T&gt;&gt; a) // this line shows the error: &quot;Erasure of method print(ArrayList&lt;ArrayList&lt;T&gt;&gt;) is the same as another method in type TBF&quot;
    {
        for(int i=0;i&lt;a.size();i++)
        print(a.get(i));
    }

    public static void main(String args[])
    {
        // some code
    }
}

This is unlike any error I received while writing the equivalent code in C++ with vector.

So I then tried this:

import java.util.*;
class TBF
{
    &lt;T&gt; void print(ArrayList&lt;T&gt; a)
    {
        for(int i=0;i&lt;a.size();i++)
        if(a.get(i) instanceof Integer || a.get(i) instanceof Long || a.get(i) instanceof String)
        System.out.print(a.get(i)+&quot; &quot;);
        else
        print(a.get(i)); // this time the error is: &quot;The method print(ArrayList&lt;T&gt;) in the type TBF is not applicable for the arguments (T)&quot;
        System.out.println();
    }

    public static void main(String args[])
    {
        // some code
    }
}

Now I can't understand how I'm supposed to print a 2D ArrayList. Please help.

答案1

得分: 1

这是Java类型擦除的经典案例(https://www.baeldung.com/java-type-erasure),这是一个概念,Java在编译时会忘记列表的具体类型,导致两个打印方法对它来说看起来是相同的。

您可以在运行时检查列表元素的类型:如果在列表内找到另一个列表,只需再次调用打印方法,针对内部列表。如果找到非列表项,只需打印它,有点像这样:

class TBF {
    void print(List<?> list) {
        for (Object o : list) {
            if (o instanceof List<?>) {
                print((List<?>) o);
            } else {
                System.out.print(o + " ");
            }
        }
        System.out.println();
    }

    public static void main(String args[]) {
        // 一些代码
    }
}
英文:

This is a classic case of Java's type erasure (https://www.baeldung.com/java-type-erasure), a concept where Java kind of forgets about the specific type of your lists at compile-time, causing both print methods to look identical to it.

What you can do is to check the type of list elements on-the-go: If you find a list inside your list, just call the print method again, for that inner list. If you find a non-list item, just print it, kinda:

class TBF {
    void print(List&lt;?&gt; list) {
        for (Object o : list) {
            if (o instanceof List&lt;?&gt;) {
                print((List&lt;?&gt;) o);
            } else {
                System.out.print(o + &quot; &quot;);
            }
        }
        System.out.println();
    }

    public static void main(String args[]) {
        // some code
    }
}

huangapple
  • 本文由 发表于 2023年6月9日 01:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定