pub struct JList<'local, 'other_local_1: 'obj_ref, 'obj_ref> { /* private fields */ }
Expand description
Wrapper for JObjects that implement java/util/List
. Provides methods to get,
add, and remove elements.
Looks up the class and method ids on creation rather than for every method call.
Implementations§
source§impl<'local, 'other_local_1: 'obj_ref, 'obj_ref> JList<'local, 'other_local_1, 'obj_ref>
impl<'local, 'other_local_1: 'obj_ref, 'obj_ref> JList<'local, 'other_local_1, 'obj_ref>
sourcepub fn from_env(
env: &mut JNIEnv<'local>,
obj: &'obj_ref JObject<'other_local_1>
) -> Result<JList<'local, 'other_local_1, 'obj_ref>>
pub fn from_env( env: &mut JNIEnv<'local>, obj: &'obj_ref JObject<'other_local_1> ) -> Result<JList<'local, 'other_local_1, 'obj_ref>>
Create a map from the environment and an object. This looks up the necessary class and method ids to call all of the methods on it so that exra work doesn’t need to be done on every method call.
sourcepub fn get<'other_local_2>(
&self,
env: &mut JNIEnv<'other_local_2>,
idx: jint
) -> Result<Option<JObject<'other_local_2>>>
pub fn get<'other_local_2>( &self, env: &mut JNIEnv<'other_local_2>, idx: jint ) -> Result<Option<JObject<'other_local_2>>>
Look up the value for a key. Returns Some
if it’s found and None
if
a null pointer would be returned.
sourcepub fn add(&self, env: &mut JNIEnv<'_>, value: &JObject<'_>) -> Result<()>
pub fn add(&self, env: &mut JNIEnv<'_>, value: &JObject<'_>) -> Result<()>
Append an element to the list
sourcepub fn insert(
&self,
env: &mut JNIEnv<'_>,
idx: jint,
value: &JObject<'_>
) -> Result<()>
pub fn insert( &self, env: &mut JNIEnv<'_>, idx: jint, value: &JObject<'_> ) -> Result<()>
Insert an element at a specific index
sourcepub fn remove<'other_local_2>(
&self,
env: &mut JNIEnv<'other_local_2>,
idx: jint
) -> Result<Option<JObject<'other_local_2>>>
pub fn remove<'other_local_2>( &self, env: &mut JNIEnv<'other_local_2>, idx: jint ) -> Result<Option<JObject<'other_local_2>>>
Remove an element from the list by index
sourcepub fn pop<'other_local_2>(
&self,
env: &mut JNIEnv<'other_local_2>
) -> Result<Option<JObject<'other_local_2>>>
pub fn pop<'other_local_2>( &self, env: &mut JNIEnv<'other_local_2> ) -> Result<Option<JObject<'other_local_2>>>
Pop the last element from the list
Note that this calls size()
to determine the last index.
sourcepub fn iter<'list>(
&'list self,
env: &mut JNIEnv<'_>
) -> Result<JListIter<'list, 'local, 'obj_ref, 'other_local_1>>
pub fn iter<'list>( &'list self, env: &mut JNIEnv<'_> ) -> Result<JListIter<'list, 'local, 'obj_ref, 'other_local_1>>
Get key/value iterator for the map. This is done by getting the
EntrySet
from java and iterating over it.
The returned iterator does not implement std::iter::Iterator
and
cannot be used with a for
loop. This is because its next
method
uses a &mut JNIEnv
to call the Java iterator. Use a while let
loop
instead:
let mut iterator = list.iter(env)?;
while let Some(obj) = iterator.next(env)? {
let obj: AutoLocal<JObject> = env.auto_local(obj);
// Do something with `obj` here.
}
Each call to next
creates a new local reference. To prevent excessive
memory usage or overflow error, the local reference should be deleted
using JNIEnv::delete_local_ref
or JNIEnv::auto_local
before the
next loop iteration. Alternatively, if the list is known to have a
small, predictable size, the loop could be wrapped in
JNIEnv::with_local_frame
to delete all of the local references at
once.