简体   繁体   English

如何在 Java 中初始化长度为 0 的字符串数组?

[英]How can I initialize a String array with length 0 in Java?

The Java Docs for the method该方法的 Java 文档
String[] java.io.File.list(FilenameFilter filter)
includes this in the returns description:在退货说明中包含此内容:

The array will be empty if the directory is empty or if no names were accepted by the filter.如果目录为空或过滤器不接受任何名称,则数组将为空。

How do I do a similar thing and initialize a String array (or any other array for that matter) to have a length 0?我如何做类似的事情并将字符串数组(或任何其他数组)初始化为长度为 0?

As others have said,正如其他人所说,

new String[0]

will indeed create an empty array.确实会创建一个空数组。 However, there's one nice thing about arrays - their size can't change, so you can always use the same empty array reference.然而,数组有一个好处——它们的大小不能改变,所以你总是可以使用相同的空数组引用。 So in your code, you can use:所以在你的代码中,你可以使用:

private static final String[] EMPTY_ARRAY = new String[0];

and then just return EMPTY_ARRAY each time you need it - there's no need to create a new object each time.然后每次需要时都返回EMPTY_ARRAY - 无需每次都创建一个新对象。

String[] str = new String[0]; ? ?

String[] str = {};

But

return {};

won't work as the type information is missing.将无法工作,因为缺少类型信息。

Ok I actually found the answer but thought I would 'import' the question into SO anyway好的,我实际上找到了答案,但我认为无论如何我都会将问题“导入”到 SO

String[] files = new String[0];
or要么
int[] files = new int[0];

You can use ArrayUtils.EMPTY_STRING_ARRAY from org.apache.commons.lang3您可以使用 org.apache.commons.lang3 中的 ArrayUtils.EMPTY_STRING_ARRAY

import org.apache.commons.lang3.ArrayUtils;

    class Scratch {
        public static void main(String[] args) {
            String[] strings = ArrayUtils.EMPTY_STRING_ARRAY;
        }
    }

Make a function which will not return null instead return an empty array you can go through below code to understand.创建一个不会返回 null 而是返回一个空数组的函数,您可以通过下面的代码来理解。

    public static String[] getJavaFileNameList(File inputDir) {
    String[] files = inputDir.list(new FilenameFilter() {
        @Override
        public boolean accept(File current, String name) {
            return new File(current, name).isFile() && (name.endsWith("java"));
        }
    });

    return files == null ? new String[0] : files;
}

You can use following things-您可以使用以下内容-

1. String[] str = new String[0];
2. String[] str = ArrayUtils.EMPTY_STRING_ARRAY;<br>

Both are same.两者都是一样的。

All you have to do is 你所要做的就是

String str = "";
String[] newString = new String[str.length()];

This will give an empty String Array. 这将给出一个空的String数组。

From my video 从我的视频

Thank you ItsPete! 谢谢你的ItPete! This answer is still important as it shows how the String is empty and how it works. 这个答案仍然很重要,因为它显示了String如何为空以及它是如何工作的。 For example, an int array cannot resize or change to another size because of that 0 that is an integer number. 例如,int数组无法调整大小或更改为其他大小,因为0是整数。 There will be an ArrayIndexOutOfBoundsException error. 将出现ArrayIndexOutOfBoundsException错误。 There must be a difference between String and int. String和int之间必须存在差异。 However again, String has the double quotations and works to resize. 但是,String再次使用双引号并且可以调整大小。 Of course, it is a longer way. 当然,这是一个更长的路。 It still shows the reasoning between the int and mainly String. 它仍然显示了int和主要String之间的推理。

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

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