- Qt 6.11
- Qt Core
- C++ Classes
- QJniArray
- Qt 6.11.1 Reference Documentation

# QJniArray Class

template <typename T> class QJniArray

The QJniArray class is a template class that represents an array in Java. More...

Header: | `#include <QJniArray>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`
Since: | Qt 6.8
Inherits: | QJniArrayBase

- List of all members, including inherited members

## Public Types

| const_iterator
| const_reverse_iterator
| iterator
| reverse_iterator

## Public Functions

| QJniArray()
| QJniArray(Container &&container)
| QJniArray(QJniArray<Other> &&other)
`(since 6.9)` | QJniArray(QJniArrayBase::size_type size)
| QJniArray(QJniObject &&object)
| QJniArray(const QJniArray<Other> &other)
| QJniArray(const QJniObject &object)
| QJniArray(jarray array)
| QJniArray(std::initializer_list<T> &list)
| ~QJniArray()
auto | arrayObject() const
QJniArray<T>::const_reference | at(QJniArrayBase::size_type i) const
QJniArray<T>::iterator | begin()
QJniArray<T>::const_iterator | begin() const
QJniArray<T>::const_iterator | cbegin() const
QJniArray<T>::const_iterator | cend() const
QJniArray<T>::const_iterator | constBegin() const
QJniArray<T>::const_iterator | constEnd() const
QJniArray<T>::const_reverse_iterator | crbegin() const
QJniArray<T>::const_reverse_iterator | crend() const
QJniArray<T>::iterator | end()
QJniArray<T>::const_iterator | end() const
QJniArray<T>::reverse_iterator | rbegin()
QJniArray<T>::const_reverse_iterator | rbegin() const
QJniArray<T>::reverse_iterator | rend()
QJniArray<T>::const_reverse_iterator | rend() const
Container | toContainer(Container &&container = {}) const
QJniArray<T> & | operator=(QJniArray<Other> &&other)
QJniArray<T> & | operator=(const QJniArray<Other> &other)
`(since 6.9)` QJniArray<T>::reference | operator[](QJniArrayBase::size_type i)
QJniArray<T>::const_reference | operator[](QJniArrayBase::size_type i) const

## Detailed Description

The QJniArray template makes it easy to work with Java methods that return or take a Java array.

Note: Java arrays can hold primitive types and objects. The array itself can be treated like a Java Object, and the JNI framework provides explicit APIs to work with such arrays. In addition, the Java class library provides container types such as `List` or `ArrayList`. Objects of those types can not be represented by a QJniArray. Instead, use QJniObject to call the class-specific member functions.

To create a QJniArray instance, either construct it from a corresponding C++ container:

     QList<int> intList;
     const QJniArray intArray = QJniArray(intList);

or from an initializer list:

     const QJniArray intArray{1, 2, 3};

QJniArray will create a new Java array and copy the C++-side data into it.

When calling functions that return an array via QJniObject::callMethod, such as `char[] toCharArray()` in the Java `String` class, specify the return type as a C array (`jchar[]` in the following):

     const auto charArray = stringObject.callMethod<jchar[]>("toCharArray");

The `charArray` variable will be of type `QJniArray<jchar>`, and hold a new global reference to the `jcharArray` JNI object.

Note that the arrays in the code snippets above are all const. Accessing elements in a const array is considerably more efficient than operating on a mutable array.

A QJniArray can also be constructed from an existing `jarray` or QJniObject. However, note that no type checking is performed to verify that the `jarray` or QJniObject indeed represents an array holding elements of the specified type, and accessing a mismatching QJniArray results in undefined behavior.

The data in a QJniArray can either be accessed element by element using at() or operator[](), or iterated over.

     for (const auto &value : array)
         process(value);

To copy the entire array into a C++ side Qt container, use the toContainer() function.

     const auto bytes = object.callMethod<jbyte[]>("getBytes");
     QByteArray data = bytes.toContainer();

which happens implicitly in

     const auto data = object.callMethod<QByteArray>("getBytes");

The return type of toContainer() depends on the type that QJniArray has been instantiated with. For `QJniArray<T>` this will typically be `QList<T>`, with the following exceptions:

Specialization | C++ type
--- | ---
QJniArray<jbyte> | QByteArray
QJniArray<char> | QByteArray
QJniArray<jstring> | QStringList
QJniArray<QString> | QStringList

An array of a fixed size can also be created without any data, and can then be populated element by element using operator[]:

     QJniArray<jint> intArray(size);
     for (int i = 0; i < size; ++i)
         intArray[i] = i;

or a mutable iterator:

     QJniArray<QString> strings(size);
     int i = 0;
     for (auto string : strings) // note: not 'auto &string'
         string = u"Row %1"_s.arg(i++);

As in Java, the size of an array can not be changed, but the array variable can be assigned to a different array.

Note: Java arrays are limited to 32 bits, and the `size_type` member type of QJniArray is `jsize`, which is a 32bit integer type. Trying to construct a QJniArray from a C++ container that holds more than 2^32 elements will cause a runtime assertion.

## Member Type Documentation

### `[alias]` QJniArray::const_iterator

### `[alias]` QJniArray::iterator

A random-access iterator for QJniArray. /*

/*!

A random-access, const iterator for QJniArray.

### `[alias]` QJniArray::const_reverse_iterator

### `[alias]` QJniArray::reverse_iterator

A reverse iterator for QJniArray, synonym for `std::reverse_iterator<iterator>`.

Note: Accessing the element of the reverse iterator using `operator->()` requires C++20, as the C++17 reverse_iterator does not support iterators that return a proxy object. /*

/*!

A reverse iterator for the QJniArray, synonym for `std::reverse_iterator<const_iterator>`.

Note: Accessing the element of the reverse iterator using `operator->()` requires C++20, as the C++17 reverse_iterator does not support iterators that return a proxy object.

## Member Function Documentation

### QJniArray::QJniArray()

Default constructor of QJniArray. This does not create a Java-side array, and the instance will be invalid.

See also isValid.

### `[explicit]` template <typename Container, QJniArrayBase::if_compatible_source_container<Container> = true> QJniArray::QJniArray(Container &&container)

Constructs a QJniArray that wraps a newly-created Java array for elements of type `Container::value_type`, and populates the Java array with the data from container.

#### Constraints

Participates in overload resolution only if `Container` is a container that stores elements of a JNI type or equivalent C++ type, and provides a forward iterator.

The specialization of the constructed QJniArray depends on the value type of the container. For a `Container<T>` (such as e.g. `QList<T>`) it will typically be `QJniArray<T>`, with the following exceptions:

Container | Specialization
--- | ---
QByteArray | QJniArray<jbyte>
QStringList | QJniArray<jstring>
Container::value_type | Specialization
QJniObject | QJniArray<jobject>

See also QJniArrayBase::fromContainer() and toContainer().

### `[noexcept]` template <typename Other, QJniArrayBase::if_convertible<Other, T> = true> QJniArray::QJniArray(QJniArray<Other> &&other)

Constructs a QJniArray by moving from other. The other array becomes invalid.

#### Constraints

Participates in overload resolution only if the element type `Other` of other is convertible to the element type `T` of the QJniArray being constructed. However, no actual conversion takes place.

### `[explicit, since 6.9]` QJniArray::QJniArray(QJniArrayBase::size_type size)

Constructs an empty QJniArray of size size. The elements in the array do not get initialized.

This function was introduced in Qt 6.9.

### `[explicit noexcept]` QJniArray::QJniArray(QJniObject &&object)

Constructs a QJniArray by moving from object. The QJniObject becomes invalid.

Note: This constructor does not perform any validation of whether the Java-side object is an array of the correct type. Accessing a mismatching QJniArray results in undefined behavior.

### template <typename Other, QJniArrayBase::if_convertible<Other, T> = true> QJniArray::QJniArray(const QJniArray<Other> &other)

Constructs a QJniArray by copying other. Both QJniArray objects will reference the same Java array object.

#### Constraints

Participates in overload resolution only if the element type `Other` of other is convertible to the element type `T` of the QJniArray being constructed. However, no actual conversion takes place.

### `[explicit]` QJniArray::QJniArray(const QJniObject &object)

Constructs a QJniArray that wraps the same Java array as object, creating a new global reference. To construct a QJniArray from an existing local reference, use a QJniObject constructed via fromLocalRef().

Note: This constructor does not perform any validation of whether the Java-side object is an array of the correct type. Accessing a mismatching QJniArray results in undefined behavior.

### `[explicit]` QJniArray::QJniArray(jarray array)

Constructs a QJniArray that wraps the Java-side array array, creating a new global reference to array.

Note: This constructor does not perform any validation of whether the Java-side object is an array of the correct type. Accessing a mismatching QJniArray results in undefined behavior.

### `[default]` QJniArray::QJniArray(std::initializer_list<T> &list)

Constructs a QJniArray that wraps a newly-created Java array for elements of type `T`, and populates the Java array with the data from list.

See also QJniArrayBase::fromContainer() and toContainer().

### QJniArray::~QJniArray()

Destroys the QJniArray object and releases any references held to the wrapped Java array.

### auto QJniArray::arrayObject() const

Returns the wrapped Java object as the suitable `jarray` type that matches the element type `T` of this QJniArray object.

T | jarray type
--- | ---
jbyte | jbyteArray
jchar | jcharArray
... | ...
jobject | jobjectArray
QJniObject | jobjectArray
Q_DECLARE_JNI_CLASS | jobjectArray

### `[noexcept]` QJniArray<T>::const_iterator QJniArray::begin() const

### `[noexcept]` QJniArray<T>::iterator QJniArray::begin()

### `[noexcept]` QJniArray<T>::const_iterator QJniArray::constBegin() const

### `[noexcept]` QJniArray<T>::const_iterator QJniArray::cbegin() const

Returns an STL-style const iterator pointing to the first item in the array.

If the array is invalid, then this will return the same iterator as the corresponding end() function.

See also end() and rbegin().

### `[noexcept]` QJniArray<T>::const_iterator QJniArray::end() const

### `[noexcept]` QJniArray<T>::iterator QJniArray::end()

### `[noexcept]` QJniArray<T>::const_iterator QJniArray::constEnd() const

### `[noexcept]` QJniArray<T>::const_iterator QJniArray::cend() const

Returns an STL-style iterator pointing just after the last item in the list.

See also begin() and rend().

### `[noexcept]` QJniArray<T>::const_reverse_iterator QJniArray::rbegin() const

### `[noexcept]` QJniArray<T>::reverse_iterator QJniArray::rbegin()

### `[noexcept]` QJniArray<T>::const_reverse_iterator QJniArray::crbegin() const

Returns an STL-style reverse iterator pointing to the first item in the array, in reverse order.

If the array is invalid, then this will return the same iterator as the corresponding rend() function.

Note: Accessing the element of the reverse iterator using `operator->()` requires C++20, as the C++17 reverse_iterator does not support iterators that return a proxy object.

See also rend() and begin().

### `[noexcept]` QJniArray<T>::const_reverse_iterator QJniArray::rend() const

### `[noexcept]` QJniArray<T>::reverse_iterator QJniArray::rend()

### `[noexcept]` QJniArray<T>::const_reverse_iterator QJniArray::crend() const

Returns an STL-style reverse iterator pointing just after the last item in the list, in reverse order.

Note: Accessing the element of the reverse iterator using `operator->()` requires C++20, as the C++17 reverse_iterator does not support iterators that return a proxy object.

See also rbegin() and end().

### template <typename Container = QJniArrayBase::ToContainerType<T>, QJniArrayBase::if_compatible_target_container<T, Container> = true> Container QJniArray::toContainer(Container &&container = {}) const

Returns a container populated with the data in the wrapped Java array.

If no container is provided, then the type of the container returned depends on the element type of this QJniArray. For `QJniArray<T>` this will typically be `QList<T>`, with the following exceptions:

Specialization | C++ type
--- | ---
QJniArray<jbyte> | QByteArray
QJniArray<char> | QByteArray
QJniArray<jstring> | QStringList
QJniArray<QString> | QStringList

If you pass in a named container (an lvalue) for container, then that container is filled, and a reference to it is returned. If you pass in a temporary container (an rvalue, incl. the default argument), then that container is filled, and returned by value.

This function returns immediately if the array is invalid.

See also QJniArrayBase::fromContainer().

### `[noexcept]` template <typename Other, QJniArrayBase::if_convertible<Other, T> = true> QJniArray<T> &QJniArray::operator=(QJniArray<Other> &&other)

Moves other into this QJniArray, and returns a reference to this. The other array becomes invalid.

#### Constraints

Participates in overload resolution only if the element type `Other` of other is convertible to the element type `T` of this QJniArray. However, no actual conversion takes place.

### template <typename Other, QJniArrayBase::if_convertible<Other, T> = true> QJniArray<T> &QJniArray::operator=(const QJniArray<Other> &other)

Assigns other to this QJniArray, and returns a reference to this. Both QJniArray objects will reference the same Java array object.

#### Constraints

Participates in overload resolution only if the element type `Other` of other is convertible to the element type `T` of this QJniArray. However, no actual conversion takes place.

### `[since 6.9]` QJniArray<T>::reference QJniArray::operator[](QJniArrayBase::size_type i)

Returns a reference object for value at position i in the wrapped Java array.

i must be a valid index position in the list (i.e., 0 <= i < size()).

The returned reference object holds the value at position i, and in most cases will convert implicitly to the value. Assigning to the returned reference will overwrite the entry in the Java array. However, calling mutating member functions on the object will not modify the entry in the array. To call member function on the result of this operator, dereference the reference object:

     QJniArray<QString> strings = object.callMethod<QString[]>("getStrings");
     if (!strings.isEmpty()) {
         if (!(*array[0]).isEmpty()) {
             // ...
         }
     }

However, if modifying the value in the array itself is not intended, make the array const, or use at() instead.

This function was introduced in Qt 6.9.

See also at() and size().

### QJniArray<T>::const_reference QJniArray::operator[](QJniArrayBase::size_type i) const

### QJniArray<T>::const_reference QJniArray::at(QJniArrayBase::size_type i) const

Returns the value at position i in the wrapped Java array.

i must be a valid index position in the list (i.e., 0 <= i < size()).

See also size().