Issue2758

classification
Title: Need Multi dimension jarrays 2D and 3D
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7
Milestone: Jython 2.7.1
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: adamburke, waynelloydsmith
Priority: Keywords:

Created on 2019-04-10.18:22:00 by waynelloydsmith, last changed 2019-04-13.11:36:45 by adamburke.

Messages
msg12432 (view) Author: Wayne Smith (waynelloydsmith) Date: 2019-04-10.18:22:00
need to be able to create 2D and 3D arrays like java Float[][][]
examples
a = jarray.array([[[1.0]*4]*2],'f')  
TypeError: can't convert [[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]] to float
a = jarray.zeros(jarray.zeros(4096*4,"f"),"f") 
   TypeError: zeros(): 1st arg can't be coerced to int
a = jarray.zeros([[[1]*4]*2],"f")  # 3D array of floats
   TypeError: zeros(): 1st arg can't be coerced to int
msg12434 (view) Author: Adam Burke (adamburke) Date: 2019-04-13.11:36:45
This should already be possible in Jython. There is an overloaded version of jarray.array() which takes a java.lang.Class object instead of a string description. As arrays are first class objects in Java, once you obtain the class object, you can pass that to create a 2D array of the appropriate type. It helps to know that "[I" is the name of the class for int[] (ie an array of int). See 10.8 here:

https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
https://www.geeksforgeeks.org/array-primitive-type-object-java/

Example using jython console

 
Jython 2.7.2a1+ (, Apr. 11 2019, 17:41:34)
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java11.0.1
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.lang import Class
>>> import jarray
>>>
>>> ci = Class.forName('[I')
>>> jarray.array([[1],[2]],ci)
array([I, [array('i', [1]), array('i', [2])])
>>>
>>> cf = Class.forName('[F')
>>> jarray.array([[1.0,1.0],[2.0,2.0]],cf)
array([F, [array('f', [1.0, 1.0]), array('f', [2.0, 2.0])])
>>>
History
Date User Action Args
2019-04-13 11:36:45adamburkesetnosy: + adamburke
messages: + msg12434
2019-04-10 18:22:00waynelloydsmithcreate