In-line Array Declarations in Method Calls
When passing an array as an argument to a method, it's common practice to declare the array explicitly before making the call. However, in some cases, it may be desirable to declare the array in-line within the method call itself.
To accomplish this, simply use the new keyword to create a new array instance and initialize it with the desired elements. For example, suppose you have a method m() that takes an array of strings as an argument. Instead of declaring a named variable for the array, you can pass an anonymous array directly to the method:
m(new String[]{"blah", "hey", "yo"});
This in-line declaration avoids the need to declare a separate variable for the array, which can be useful when the array is used only once and does not need to be referenced later in the code.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3