Tuesday, October 20, 2009

Java Arrays and jarray




I l@ve RuBoard









Java Arrays and jarray


The closest thing to a Java array in Python is a tuple. Remember that a tuple is immutable and a list is mutable. Like a list, a Java array is mutable but, like a tuple, it's of fixed length. You may think that a tuple or a list is a likely candidate for an array, but that's not exactly the case. You see, an array is like a homogeneous tuple or list�every item in it is of the same type; but a tuple is heterogeneous�items in it can be of different types. A lot of Java methods have Java arrays as arguments, so when you pass an array to a Java method, Python has to guarantee that it will be homogeneously typed.


Just a note: Because they're strongly typed, Java arrays can hold Java objects or primitive types.


Jython adds PyArray class support for Java arrays�org.python.core.PyArray to be exact. Instances of PyArray are returned from Java methods that return Java arrays. You need to create instances of PyArray to pass to Java methods that require arrays. Python makes creating a PyArray painless with the jarray module, which defines two public functions, described in Table 11-4.



No Built-In Java Objects


A Java object is only an instance of a class. There are no other object types, so there are no built-in Java objects as there are in Python. The closest things Java has to Python's built-in objects are strings and arrays, which require special syntax.


















Table 11-4. jarray Public Functions
Function
Description
array(sequence, type)
Creates an array of a specific type with values from the sequence; the array is the same size as the sequence
zeros(length, type)
Creates an empty array the length of the length parameter


The type argument for both zeros() and array() can be either a string or a Java class object. You can use a single-character string typecode to specify that the array will hold primitive values. Table 11-5 shows the typecodes and their primitive types.


Here's an example of creating arrays with the array() and zeros() methods from the jarray module.


Create a Boolean array using the array() method.



>>> from jarray import array, zeros
>>> seq = (1,0,1,0,1,0)
>>> booleanArray = array(seq, 'z')

Inspect its contents.



>>> booleanArray
array([1, 0, 1, 0, 1, 0], boolean)

>>> print `booleanArray`
array([1, 1, 1, 1, 1, 0], boolean)

See what type it is.



>>> booleanArray
array([1, 0, 1, 0, 1, 0], boolean)

>>> print `booleanArray`
array([1, 1, 1, 1, 1, 0], boolean)








































Table 11-5. Typecodes and Primitive types
Typecode
Primitive Type
'z'
boolean
'c'
char
'b'
byte
'h'
short
'i'
int
'l'
long
'f'
float
'd'
double


Change its values.



>>> booleanArray[1]=1
>>> booleanArray[3]=1
>>> booleanArray
array([1, 1, 1, 1, 1, 0], boolean)

You can use the same operations on an array that you use on a sequence (a tuple or a list). Here's an example:



>>> for boolean in booleanArray:
... print `boolean`
...
1
1
1
1
1
0
>>> len(booleanArray)
6

Even though an array is mutable, it's still of fixed length; that means that you can't append to it as you can a Python list. If you try, you'll get an attribute error.



>>> booleanArray.append(1)
Traceback (innermost last):
File "<console>", line 1, in ?
AttributeError: instance of 'org.python.core.PyArray' has no attribute 'append'

Let's create an array with the zeros() method.



>>> IntArray = zeros(10,'i')
>>> print IntArray
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], int)

As the above example shows, you can think of PyArray essentially as a Python built-in type that's specific to Jython. Like other features Jython has for integrating with Java, PyArrays are transparent to the programmer (when returned from a Java method). Creating one is easy with Jython's jarray module.








    I l@ve RuBoard



    No comments: