Working with arrays

You are browsing legacy Javonet 1.5 (Java<>.NET bridge for Windows) documentation. Use the left side menu or click here to switch to latest Javonet 2.0 documentation. Javonet 2.0 allows you to use any module from JVM, CLR, Netcore, Python, Ruby, Perl, NodeJS on Windows, Linux and MacOs from any application created in Java, Clojure, Groovy, Kotlin, C#, F#, J#, VB.NET, Python, Perl, Ruby, JavaScript, TypeScript, C++ and GoLang

Data structures are one of the essential aspects of every piece of software that has ever been written. Any application, from simple command line util to scalable enterprise systems, constantly process various information, that very often require specific grouping and access strategies. This aspect is addressed by arrays and more advanced collection types available both in Java and .NET environments. By using the Javonet framework, users gain ability to easily and effectively work with data structures originating from the other platform.

I'll be working with the following pseudo-service class, which main purpose is to produce and consume various array-typed objects.

This snippet doesn't support selected combination of technologies.

Retrieving and handling array from JAR library

Javonet framework allows for simple retrieval of arrays of objects, as presented in the following examples. Such objects are treated as usual Java arrays, therefore all the standard Java programming techniques apply. Although examples present objects' retrieval in result to method calls, the same approach is applicable for getting class properties, direct member access etc.

For Java developers - It's worth mentioning that, when dealing with primitive, value-typed .NET arrays, Javonet framework automatically translates them to respective Java primitive type. In such cases, it is necessary to keep in mind, that the returned array will contain boxing-type objects, otherwise we might face an InvalidClassCastException.

Example of calling method which returns array of Integers:

I code in:

This snippet doesn't support selected combination of technologies.

As for arrays containing reference-type objects, we're simply dealing with an NObject/JObject array. Working with such object is no different that for any other NObject/JObject provided by the framework from the JAR library

Example of calling method which returns array of Objects:

I code in:

This snippet doesn't support selected combination of technologies.

In the previous examples, we've been retrieving a single-dimensional arrays. It is possible however, to get an array of arrays, either for primitive- and reference-type objects. Here are the examples:

Value-typed nested arrays:

I code in:

This snippet doesn't support selected combination of technologies.

Reference-type nested arrays:

I code in:

This snippet doesn't support selected combination of technologies.

For Java developers - Unfortunately, current version of Javonet framework does not support retrieval of arrays containing arraytype objects, nested more than once. Also, the multidimensional arrays (in the .NET way of things) are not supported either. Thankfully, despite the fact, that such cases are not that common and/or easy workarounds exist, it is considered as viable enhancement, which most probably will be shipped in one of the upcoming versions.

Passing arrays

For Java developers - Being aware of the automatic type conversion for value-typed arrays, it is absolutely safe to pass regular Java arrays as arguments to the .NET code. Similarily as when retrieving primitive arrays, it is necessary to work with the boxed wrappers.

Passing array of primitive types:

I code in:

This snippet doesn't support selected combination of technologies.

In case we need to send a reference-type array, all we need to do is create an array of NObjects/JObjects and populate it, as shown in the next example.

I code in:

This snippet doesn't support selected combination of technologies.

Unlike the array-retrieval scenario, sending nested arrays is not supported at the moment, it is however considered for shipment, depending on the demand for such feature.

Summary

In this article we've focused on working with arrays using Javonet framework. As for array objects, differences of handling arrays of value- and reference-type objects have been discussed, as well as framework's current limitations have been presented.