简体   繁体   English

我们可以通过在 Java 中使用变长参数来实现动态数组吗?

[英]Can we achieve a dynamic array by using variable length arguments in Java?

My question is in java we know we can't create dynamic arrays , because when ever we are going to initialize values for array indexes before that we need to define the array size.我的问题是在 Java 中我们知道我们不能创建动态数组,因为在我们需要定义数组大小之前,我们要初始化数组索引的值。 But we all know that there is a java feature called variable length arguments ,which will create a dynamic array.但是我们都知道java有一个特性叫做变长参数,它会创建一个动态数组。

Best Ex:public static void main (String... args) So using this variable length arguments we can actually insert any amount of elements for the args array.最好的例子:public static void main (String... args)所以使用这个可变长度参数,我们实际上可以为 args 数组插入任意数量的元素。 What is this contradiction ,basic rules of java saying you can't and but another feature enables to do so.这个矛盾是什么,java的基本规则说你不能,但另一个特性可以这样做。

But we all know that there is a java feature called variable length arguments ,which will create a dynamic array.但是我们都知道java有一个特性叫做变长参数,它会创建一个动态数组。

No, it won't.不,不会。 It will create an array of a static size, and that static size is defined (in your example) by the number of command line arguments.它将创建一个静态大小的数组,并且该静态大小(在您的示例中)由命令行参数的数量定义。 There's no way to increase or decrease the size of this array after it's created.创建后,无法增加或减少此数组的大小。 This is really no different than creating a fixed array of a size you calculate programmatically.这实际上与创建一个您以编程方式计算的大小的固定数组没有什么不同。

If you want an array of a dynamic (ie changing size) then Java doesn't offer that.如果您想要一个动态数组(即更改大小),那么 Java 不提供。 You'll need to use a list, or another collection, instead.您需要改用列表或其他集合。

When it comes to vararg methods, you can think of them as methods that accept an array:当涉及到可变参数方法时,您可以将它们视为接受数组的方法:

public void f(String... args)

is the same as是相同的

public void f(String [] args) 

This is merely a syntactic sugar that allows the method defined with varags to be called as:这只是一个语法糖,它允许使用 varags 定义的方法被调用为:

f("a","b","c")

Instead of creating an array first而不是先创建一个数组

But if so, you are supposed to specify the length of array when you create it (outside) this specific function f will accepts arrays of any length in runtime但是如果是这样,您应该在创建数组时(在外部)指定数组的长度,此特定函数 f 将在运行时接受任何长度的数组

There is no contradiction.没有矛盾。 What doesn't change is the size of an array that has been created.不变的是已创建数组的大小。

So using this variable length arguments we can actually insert any amount of elements for the args array因此,使用这个可变长度参数,我们实际上可以为 args 数组插入任意数量的元素

Yes, (String... args) allows you to specify any size for the array, just as:是的, (String... args)允许您为数组指定任何大小,就像:

main(String[] args) //called with (new String[]{"a", "b", "c"})

Lets you choose any size for args .允许您为args选择任何大小。 You do the same thing by calling main(String...) with你通过调用main(String...)来做同样的事情

main("a", "b", "c")

What you don't realize is that neither of the resulting arrays can change in size.您没有意识到结果数组的大小都不能改变。 The size of the array is only dynamic as far as it does not need to be known at compile time, but once created (at runtime), the size of the array cannot be changed.只要在编译时不需要知道数组的大小,数组的大小就是动态的,但是一旦创建(在运行时),数组的大小就无法更改。

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

相关问题 使用可变长度参数进行Java重载 - Java overloading with variable length arguments Scala的Java可变长度参数 - Java Variable Length Arguments to Scala 如何使用可变长度参数为Java函数动态传递参数 - How to dynamically pass arguments for a java function using variable length parameter "我可以将数组作为参数传递给 Java 中具有可变参数的方法吗?" - Can I pass an array as arguments to a method with variable arguments in Java? Java变量数组长度 - Java Variable array length Java中的可变长度(动态)数组 - Variable length (Dynamic) Arrays in Java 为什么在Java中可以使用变量作为长度来初始化数组? - Why can an array can be initialized with a variable as length in java? “可变长度参数”在 Java 中如何工作? - How does 'variable length arguments' work in Java? 使用Java将大量的Insert和Save组合到mongodb中? 有没有办法可以实现这一目标? - Combining bulk Insert and Save in mongodb using Java? Is there a way through which we can achieve it? 我们如何在不使用任何内置函数的情况下找到Java中String的长度? (甚至不包括charAt()或toCharArray()或length()) - How can we find the length of a String in Java without using any inbuilt functions? (not even charAt() or toCharArray() or length())
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM