I am new to Java and this I have encountered several functions that accept an array of given elements (e.g. int[]
). However, there are cases where I just have one int to pass and I was wondering how to do this inline (e.g. without defining an array variable first).
For example, how to simplify this:
int[] pidArray = { mySinglePID };
am.getProcessMemoryInfo(pidArray); // This one accepts arrays only
To something like (made up, doesn't work this way):
am.getProcessMemoryInfo([mySinglePID]);
Just use Anonymous Array for your code:
am.getProcessMemoryInfo(new int[]{mySinglePID }); // This one accepts arrays only
Anonymous Array: In java it is perfectly legal to create an anonymous array using the following syntax.
new <type>[] { <list of values>};
try
int[] pidArray = new int[]{ mySinglePID };
am.getProcessMemoryInfo(pidArray);
oneliner would be
am.getProcessMemoryInfo(new int[]{mySinglePID });
You can do it like this: -
am.getProcessMemoryInfo(new int[]{ mySinglePID });
So, you don't need to declare your array variable here. Just pass an unnamed array
object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With