How to pass a function to an array literal?


other

How can I pass an array without making it a separate variable? For example, I know this works:

class Test{
    public static void main(String[] args){
        String[] arbitraryStrings={"foo"};
        takesStringArray(arbitraryStrings);
    }
    public static void takesStringArray(String[] argument){
        System.out.println(argument);
    }
}

But I don't want to make the array a variable since it's only used here. Is there any way to do something like this:

class Test{
    public static void main(String[] args){
        takesStringArray({"foo"});
    }
    public static void takesStringArray(String[] argument){
        System.out.println(argument);
    }
}
crazy programmer

{"foo"}Without telling Java what type of array you want to create...

Instead, try something like...

takesStringArray(new String[] {"foo"});

Related


How to pass parameters in function object literal

CalAlt I want to pass integers as min and max parameters to a function to return a random number for use by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not a functi

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass parameters in function object literal

CalAlt I would like to pass integers as min and max parameters to a function to return a random number to be used by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass parameters in function object literal

CalAlt I would like to pass integers as min and max parameters to a function to return a random number to be used by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not

How to pass parameters in function object literal

CalAlt I would like to pass integers as min and max parameters to a function to return a random number to be used by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass an array to a function

User 10828115 I have got$array = ["banana", "kiwi", "apple"]; I want to pass it to myFunction()so that I can use it in that function. I did some searching and found globalbut it doesn't work. function myFunction() { global $array; foreach ($

How to pass an array to a function

User 10828115 I have got$array = ["banana", "kiwi", "apple"]; I want to pass it to myFunction()so that I can use it in that function. I did some searching and found globalbut it doesn't work. function myFunction() { global $array; foreach ($

cstr literal in C++11, how to pass (char*[]) to function

Johannes When wrapping an OpenGL shader, I found a problem with one of the required parameters. The source of the standard vertex shader is copied into the internal representation by OpenGL. A simple example might look like this: static const GLchar * vertex_s

cstr literal in C++11, how to pass (char*[]) to function

Johannes When wrapping an OpenGL shader, I found a problem with one of the required parameters. The source of the standard vertex shader is copied into the internal representation by OpenGL. A simple example might look like this: static const GLchar * vertex_s

Pass string literal to template function

scarecrow I try to specifically target several types of template functions, one of which is const char*. template<typename T = const char*> void f(T&&); template<> void f<int>(int&& k) { std::cout << "Now in int fcn (" << k << ")!\n"; } template<> void f<con

Pass string literal to template function

scarecrow I try to specifically target several types of template functions, one of which is const char*. template<typename T = const char*> void f(T&&); template<> void f<int>(int&& k) { std::cout << "Now in int fcn (" << k << ")!\n"; } template<> void f<con

Pass string literal to template function

scarecrow I try to specifically target several types of template functions, one of which is const char*. template<typename T = const char*> void f(T&&); template<> void f<int>(int&& k) { std::cout << "Now in int fcn (" << k << ")!\n"; } template<> void f<con

Pass string literal to template function

scarecrow I try to specifically target several types of template functions, one of which is const char*. template<typename T = const char*> void f(T&&); template<> void f<int>(int&& k) { std::cout << "Now in int fcn (" << k << ")!\n"; } template<> void f<con

Pass string literal to template function

scarecrow I try to specifically target several types of template functions, one of which is const char*. template<typename T = const char*> void f(T&&); template<> void f<int>(int&& k) { std::cout << "Now in int fcn (" << k << ")!\n"; } template<> void f<con

Pass string literal to template function

scarecrow I try to specifically target several types of template functions, one of which is const char*. template<typename T = const char*> void f(T&&); template<> void f<int>(int&& k) { std::cout << "Now in int fcn (" << k << ")!\n"; } template<> void f<con

How to pass an array to a function in VBA?

username I am trying to write a function that accepts an array as a parameter. The array can have any number of elements. Function processArr(Arr() As Variant) As String Dim N As Variant dim finalStr as string For N = LBound(Arr) To UBound(

How to pass a multidimensional array to a function?

Yu Zhang In C#, I need to pass a multidimensional integer array to a method, but the actual dimensions of the array are not known until runtime, how to achieve this? For example, the input could be: {1,2,3} {1,2,{1}} {1 {1,2,3,} 2,2,{6,{4,{1,2,3,4}}}}} Basical

How to pass array items to function?

Marcio Quitke I am trying to call two functions, the first function will receive the data and the second function will display the data. But my second function is equal to the first, so it doesn't do what I intended. Any suggestions? struct Operador { c

How to pass an array structure to a function?

Marcio Quitke I'm trying to pass an array of extracts to a function, but I just gave up. I'm trying to store data in a struct function "fun()" so that I can extract it later in the function lo(); when I need it too. #include<stdio.h> #include<string.h> struc

How to pass an array parameter to a function

Peter XX I have implemented a simple merge sort function in PowerShell as follows function Merge-Sort { param($a) if ($a.Length -gt 1) { $m = [Math]::floor($a.Length / 2) [Int[]]$l = $a[0..($m-1)] [Int[]]$r = $a[$m..($a.Length)] Merge-So