Differences between revisions 3 and 4
Revision 3 as of 2007-10-19 22:33:31
Size: 1780
Editor: GregMoore
Comment:
Revision 4 as of 2007-10-29 00:47:54
Size: 2520
Editor: AlfonsoReyes
Comment:
Deletions are marked like this. Additions are marked like this.
Line 62: Line 62:

=== Multidimensional 2-D String Array ===
In case you need 2-D string arrays this example may help you to start. Cheers! Alfonso Reyes, Houston-Texas

{{{#!python
from java.lang.reflect import Array
import java

rows = 3
cols = 3

str2d = java.lang.reflect.Array.newInstance(java.lang.String,[rows, cols])
str2d[0][0] = "python"
str2d[1][0] = "jython"
str2d[2][0] = "java"

str2d[0][1] = "syntax "
str2d[1][1] = "strength"
str2d[2][1] = "libraries"

str2d[0][2] = "unclutter"
str2d[1][2] = "combine"
str2d[2][2] = "graphics"

print str2d

print "printing multidimensional array"
for i in range(0, len(str2d), 1):
    for j in range(0, len(str2d[i]), 1):
        print str2d[i][j]+"\t",
    print
print
}}}

Core Jython / Python Examples

DocumentationAndEducation

TableOfContents


Examples related to core Jython / Python will be here. Intended for those new to Python / Jython

Print

Print Hello world {{{ print 'Hello World' print "Hello World" }}}

JArray

I was curious on how to work with jarray and move the arrays between classes after being filled with values and operated. I think this example explains some of the basics. Of course, there are more pythonic ways to do this. Cheers! Alfonso Reyes

   1 from java.lang import Math
   2 from jarray import array, zeros
   3 import java.util.Random;
   4 
   5 class ArrayClass:
   6     """    This class will fill an array with random numbers
   7     and then return the array for further operations
   8     """
   9     def __init__(self, elems):
  10         self.N = elems
  11         systemEnergy = 0.0025
  12         self.v = zeros(self.N, "d")     # array of zeros, double type
  13         v0 = Math.sqrt(2.0 * systemEnergy / self.N)
  14         for i in range(0, self.N, 1):
  15             r = java.util.Random()
  16             self.v[i] = v0 * r.nextInt(self.N)  # same velocity for all particles
  17 
  18     def out(self):
  19         for i in range(0, self.N, 1):
  20             print i, self.v[i]
  21 
  22     def get(self):
  23         return self.v
  24 
  25 n = 100
  26 uarr = zeros(n, "d")    # array of double to store some operations
  27 ac = ArrayClass(n)      
  28 ac.out()                # print the array
  29 arr = ac.get()          # get the array to start doing some work on it
  30 
  31 # get a first third of the array members and times 10
  32 print "Get a first third of the array members"
  33 for i in range(0, n/3, 1):
  34     uarr[i] = arr[i] * 10
  35     print i, arr[i], uarr[i]

Multidimensional 2-D String Array

In case you need 2-D string arrays this example may help you to start. Cheers! Alfonso Reyes, Houston-Texas

   1 from java.lang.reflect import Array
   2 import java
   3 
   4 rows = 3
   5 cols = 3
   6 
   7 str2d = java.lang.reflect.Array.newInstance(java.lang.String,[rows, cols])
   8 str2d[0][0] = "python"
   9 str2d[1][0] = "jython"
  10 str2d[2][0] = "java"
  11 
  12 str2d[0][1] = "syntax "
  13 str2d[1][1] = "strength"
  14 str2d[2][1] = "libraries"
  15 
  16 str2d[0][2] = "unclutter"
  17 str2d[1][2] = "combine"
  18 str2d[2][2] = "graphics"
  19 
  20 print str2d
  21 
  22 print "printing multidimensional array"
  23 for i in range(0, len(str2d), 1):
  24     for j in range(0, len(str2d[i]), 1):
  25         print str2d[i][j]+"\t",
  26     print
  27 print

CoreJythonExamples (last edited 2010-03-13 11:08:47 by s235-148)