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.

namespace TestNamespace
{
	class ArrayService
	{
		public int[] GetIntArray()
		{
			return new int[] { 1, 2, 3, 4 };
		}

		public void UseIntArray(int[] intArray)
		{
			System.Console.WriteLine("[.NET] Contents of primitive-type array:");
			foreach (int intVal in intArray)
			{
				System.Console.Write("{0} ", intVal);
			}
		}

		public int[][] GetArrayOfIntArrays()
		{
			int[][] arrayOfIntArrays = new int[2][];

			arrayOfIntArrays[0] = new int[4] { 1, 2, 3, 4 };
			arrayOfIntArrays[1] = new int[4] { 11, 22, 33, 44 };

			return arrayOfIntArrays;
		}

		public CustomObject[] GetCustomObjectArray()
		{
			return new CustomObject[]{ new CustomObject("A",1),
						new CustomObject("B", 2),
						new CustomObject("C", 3)};
		}

		public void UseCustomObjectArray(CustomObject[] cuArray)
		{
			System.Console.WriteLine("[.NET] Contents of the reference-type array:");
			foreach (CustomObject cu in cuArray)
			{
				System.Console.Write("{0}[{1}] ", cu.Name, cu.Value);
			}
		}

		public CustomObject[][] GetArrayOfCustomObjectArrays()
		{
			CustomObject[][] arrayOfCustomObjectArrays = new CustomObject[2][];

			arrayOfCustomObjectArrays[0] = new CustomObject[]{
					new CustomObject("A1",1),
					new CustomObject("A2", 2),
					new CustomObject("A3", 3)};

			arrayOfCustomObjectArrays[1] = new CustomObject[]{
					new CustomObject("B1",1),
					new CustomObject("B2", 2),
					new CustomObject("B3", 3)};

			return arrayOfCustomObjectArrays;
		}
	}

	class CustomObject
	{
		public string Name { get; set; }
		public int Value { get; set; }

		public CustomObject(string name, int value)
		{
			this.Name = name;
			this.Value = value;
		}
	}
}

Retrieving and handling array from .NET Framework DLL

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:
// Todo: activate Javonet and add reference to .NET library

NObject arrayService = Javonet.New("TestNamespace.ArrayService");

Integer[] intArray = arrayService.invoke("GetIntArray");

System.out.format("[Java] Contents of primitive-type array: %n");

for (int intVal : intArray) {
    System.out.print(intVal + "\t");
}
// expected output
// [Java] Contents of primitive-type array:
// 1    2   3   4

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 .NET Framework DLL

Example of calling method which returns array of Objects:

I code in:
// Todo: activate Javonet and add reference to .NET library

NObject arrayService = Javonet.New("TestNamespace.ArrayService");
NObject[] coArray = arrayService.invoke("GetCustomObjectArray");

System.out.format("[Java] Contents of reference-type array: %n");
for (NObject co : coArray) {
    System.out.format("%s[%d]  ", co.get("Name"), co.get("Value"));
}
// expected output:
// [Java] Contents of reference-type array:
// A[1]  B[2]  C[3]

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:
// Todo: activate Javonet and add reference to .NET library

NObject arrayService = Javonet.New("TestNamespace.ArrayService");
NObject[] arrayOfIntArrays = arrayService.invoke("GetArrayOfIntArrays");

System.out.format("[Java] Contents of primitive-type array of arrays: %n");

for (NObject intArray : arrayOfIntArrays) {
    for (int i = 0; i < intArray.<Integer>get("Length"); i++) {
        System.out.format("%s ", intArray.<Integer>getIndex(i));
    }
    System.out.println();
}
// expected output:
// [Java] Contents of primitive-type array of arrays:
// 1 2 3 4
// 11 22 33 44

Reference-type nested arrays:

I code in:
// Todo: activate Javonet and add reference to .NET library

NObject arrayService = Javonet.New("TestNamespace.ArrayService");
NObject[] arrayOfCOArrays = arrayService.invoke("GetArrayOfCustomObjectArrays");

System.out.format("[Java] Contents of reference-type array of arrays: %n");

for (NObject coArray : arrayOfCOArrays) {
    for (int i = 0; i < coArray.<Integer>get("Length"); i++) {
        NObject co = coArray.getIndex(i);
        System.out.format("%s[%d] ", co.get("Name"), co.get("Value"));
    }
    System.out.println();
}
// expected output:
// [Java] Contents of reference-type array of arrays:
// A1[1] A2[2] A3[3]
// B1[1] B2[2] B3[3]

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:
// Todo: activate Javonet and add reference to .NET library

NObject arrayService = Javonet.New("TestNamespace.ArrayService");
Integer[] intArray = {1, 2, 3, 4};
arrayService.invoke("UseIntArray", new Object[]{intArray});
// expected output:
// [.NET] Contents of primitive-type array:
// 1 2 3 4

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:
// Todo: activate Javonet and add reference to .NET library

NObject arrayService = Javonet.New("TestNamespace.ArrayService");
NObject[] cuArray = {Javonet.New("CustomObject", "D", 1), Javonet.New("CustomObject", "E", 2)};
arrayService.invoke("UseCustomObjectArray", new Object[]{cuArray});
// expected output:
// [.NET] Contents of the reference-type array:
// D[1] E[2]

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.