java 打印数组中所有元素的通用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5861926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:09:46  来源:igfitidea点击:

generic method to print all elements in an array

javaarraysgenericsmethods

提问by user685275

I wanna a method that would loop any type array and print them, I have written the following:

我想要一个可以循环任何类型数组并打印它们的方法,我写了以下内容:

public static <T> void printArray(T[] arr){
    for(T t: arr){
       System.out.print(t+" ");
    }
    System.out.println("");
}

but this one only works for class arrays, what if I have a char[]instead of a Character[], or a int[]instead of an Integer[], or is there a way to cast them before hand? Thanks

但是这个只适用于类数组,如果我用 achar[]代替 a Character[],或者用 aint[]代替 an Integer[],或者有没有办法事先投射它们呢?谢谢

回答by Bozho

java.util.Arrays.toString(array)should do.

java.util.Arrays.toString(array)应该做。

  • commons-langalso have that - ArrayUtils.toString(array)(but prefer the JDK one)
  • commons-langallows for custom separator - StringUtils.join(array, ',')
  • guavaalso allows a separator, and has the option to skip null values: Joiner.on(',').skipNulls().join(array)
  • commons-lang也有 - ArrayUtils.toString(array)(但更喜欢 JDK 之一)
  • commons-lang允许自定义分隔符 -StringUtils.join(array, ',')
  • 番石榴还允许使用分隔符,并且可以选择跳过空值:Joiner.on(',').skipNulls().join(array)

All of these return a String, which you can then System.out.println(..)or logger.debug(..). Note that these will give you meaningful input if the elements of the array have implemented toString()in a meaningful way.

所有这些都返回 a String,然后您可以使用System.out.println(..)logger.debug(..)。请注意,如果数组的元素toString()以有意义的方式实现,这些将为您提供有意义的输入。

The last two options, alas, don't have support for primitive arrays, but are nice options to know.

最后两个选项,唉,不支持原始数组,但知道是不错的选择。

回答by Aravindan R

You cant write a generic definition for primitive arrays. Instead, you can use method overloading and write a method for each primitive array type like this,

您不能为原始数组编写通用定义。相反,您可以使用方法重载并为每个基本数组类型编写一个方法,如下所示,

public static void printArray(int[] arr)
public static void printArray(short[] arr)
public static void printArray(long[] arr)
public static void printArray(double[] arr)
public static void printArray(float[] arr)
public static void printArray(char[] arr)
public static void printArray(byte[] arr)
public static void printArray(boolean[] arr)

回答by Pradeep C

private static void printArray(Object arr) {
        // TODO Auto-generated method stub
        String arrayClassName=arr.getClass().getSimpleName();
        if (arrayClassName.equals("int[]"))
            System.out.println(java.util.Arrays.toString((int[]) arr));
        if (arrayClassName.equals("char[]"))
            System.out.println(java.util.Arrays.toString((char[]) arr));
    }

回答by Kal

You can't pass primitive arrays to the printArray() method

您不能将原始数组传递给 printArray() 方法