Home www.python.org
Download Documentation
Jython 2.0
Jython 2.1
JVM Compatibility
Executive Summary
Invoking Jython
Jython Registry
Compiling Jython from source
Working with Java
The Basics
JavaBean Properties
Java arrays
Building applets, servlets, beans...
Reloading java classes
Python Docs (exits)
Python Tutorial
Library Reference
Jython vs. CPython
Jython FAQ
List Archives (exit)
JPython paper (exit)
Email Us
SourceForge Logo

Java Arrays in Jython - JArray

Many Java methods require Java array objects as arguments. The way that these arguments are used means that they must correspond to fixed-length, mutable sequences, sometimes of primitive data types. The PyArray class is added to support these Java arrays and instances of this class will be automatically returned from any Java method call that produces an array. In addition, the "jarray" module is provided to allow users of Jython to create these arrays themselves, primarily for the purpose of passing them to a Java method.

The jarray module exports two functions:

array(sequence, type)
zeros(length, type)

array will create a new array of the same length as the input sequence and will populate it with the values in sequence. zeros will create a new array of the given length filled with zeros (or null's if appropriate).

type can either be a single character typecode (using the same mappings as Python's array module) or it can be an instance of a JavaClass object. The valid typecodes are shown in the following table:

Character Typecode Corresponding Java Type
z boolean
c char
b byte
h short
i int
l long
f float
d double


>>> from jarray import zeros, array
>>> array([1,2,3], 'd')
array([1.0, 2.0, 3.0], double)
>>> zeros(3, 'f')
array([0.0, 0.0, 0.0], float)
>>> from java.util import Hashtable
>>> array([Hashtable(), Hashtable()], Hashtable)
array([<java.util.Hashtable instance at 2045730>, <java.util.Hashtable instance at 2045714>], java.util.Hashtable)
>>> zeros(5, Hashtable)
array([None, None, None, None, None], java.util.Hashtable)
This example show how to create an array of three specific doubles, a length-three array of floats, an array of two specific instance of java.util.Hashtable, and an empty array of java.util.Hashtable's.