Jython Convert List to Array - java

I have a java function I'm trying to call whose signature is:
void doStuff(Object...args);
I want to call that function from within Jython, however I have my args in a list. I can't find any way to convert my list to an array so I can call this java function that has a variable number of arguments.

I would suggest to go through you list step by step.
And with every value found, ad that one to the array.
for x in list:
array.append(x)

Related

Java get specific element from Object[]

How can I print the second element from the below Object[] args? Is there a way to get it using Arrays.toString(args). I want to get only the 2nd element sayHello
[com.example:type=Hello, sayHello, [Ljava.lang.Object;#1503f191, [Ljava.lang.String;#6229b4c0]
Arrays are objects that can be manipulated by indices too, those indices are integers pointing to its location in the object, furthermore, they are zero based, which means, the 1st element is located at index 0
following the illustration above, what you need is to do Object foo = args[1];
or invoke directly a method if required, e.g. args[1].toString();

Passing on a Multi-Dimensional Array to another Method in Java

I have numbers[x][y] and int pm2 = 0;. Is there a way to pass on this Mult-Array onto public static boolean checkNumber(int[] list, int num)? <- the parameters has to be used this way.
I invoked checkNumber(numbers[x][y], pm2);
I need to use the checkNumber method to check if a number has already been entered and returns true if the number is present and false if number is absent.
I am allowed to use multiple methods thought so I did have an idea of doing numbers[x][0] , numbers[x][1] etc, etc and invoking them into multiple checkNumber() methods. I was just wondering if there's a shorter way.
You have single dimensional array as parameter.
So you have to pass one at a time probably in loop.
I was just wondering if there's a shorter way.
No there isn't. The Java language doesn't support any kind of array "slicing", and you can't subvert the type system to allow you to refer use an array with a different type to what it really has.
You need to implement your idea of iterating the int[] component array of the int[][], passing each one to checkNumber(int[], int). Something like this:
for (int[] subarray : numbers) {
checkNumbers(subarray, pm2);
}

create a two dimensionla array with Java reflection using java.lang.reflect.Array.newInstance

I need to create a two dimensional array of type java.lang.String using the reflection method from JavaScript code which is running inside a java application (inside the rhino scripting engine). This array will be a return value (of a javaScript function) that is used from JavaCode after the function call.
function test() {
var a = java.lang.reflect.Array.newInstance(?, ?);
// fill the array
return a;
}
I couldn't find the right parameters for the newInstance call to create a two diemnsional array of type String.
At the moment I'm working with a workaround, i.e. I create an (outer) array of type java.lang.Objectof size x and inside a x-length loop a create multiple arrays of type java.lang.String each of size y which are assigned to the ''outer' array elements.
Is there an easier way?
All you have to do is fill in the class and dimensions:
var a = java.lang.reflect.Array.newInstance(String.class, x, y);
Read more in the javadoc for newInstance(Class<?> componentType, int... dimensions).

Using an array of custom objects

Let's say I've created an array of objects SpecialResource via
ArrayList<SpecialResource> masterRes = new ArrayList<SpecialResource>();
masterRes.add(0, new SpecialResource(3,5,0,"Foo Bar"));
.........etc etc many more adds...
Let's say SpecialResource has a method calledgetMax()
How would I reference the getMax method of array index 0? Every permutation I've guessed at is giving syntax errors. masterRes<0>.getMax(), masterRes(0).getMax(), etc.
Well, actually, it's not an array, but a collection. And, in order to retrieve its items by index, you must use the get method:
masterRes.get(0).getMax();
masterRes.get(0).getMax();
Java/Android API document will help you.
http://androidappdocs.appspot.com/reference/java/util/ArrayList.html

Meaning of int... or N... in java

I have a function from a library whose signature says:-
public void setColumnNames(N... columnNames);
1.) What is the meaning of 'N...' ?
Also I have a list like this:-
List<HColumn<String,String>>
I want to extract the 1st String of each element HColumn of this list and pass all these Strings as a single argument in above function. I am doing this job to compute the things that need to be displayed on a page of a website. Thus I need a superfast method to do so.
2.) How do I go for it ??
public void setColumnNames(N... columnNames)
means that setColumnNames takes any number of arguments of type N.
This feature is called varargs.
Taking glowcoder's suggestion, here's the other part:
2) Build an array of type N[] with the same length as the list, transfer the strings from the list to the array (converting them from String to N, however that's done), and pass the array as the argument to the function.

Categories

Resources