SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 178 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"

to_java :char # new char[] {1,2,3}
[1,2,3].to_java :float # new float[] {1,2,3}
# etc
["str", "str2"].to_java java.lang.String # new String[]{"str","str2"}
As you can see, you can provide either a symbol representing the primitive type, or a class
instance. The primitive types you can use are :boolean, :byte, :char, :double, :float, :int,
:long, and :short. There are also shortcuts for java.lang.Object, java.lang.String,
java.math.BigDecimal, and java.math.BigInteger, as well as for by :object, :string,
:big_decimal or :decimal, and :big_integer or :big_int.
In most cases, this is all you need. However, sometimes you might need to create an
empty array, or an array of more than one dimension. In these cases there are [] helpers on all
Java classes in the system. So, for example, you could create an empty String[] with length 3
like this:
java.lang.String[].new 3
Or you could create it like this:
java.lang.String[3].new
If you need a two dimensional array, you can just add a new parameter to the call. For
example, you do this to create something akin to new String[3][3]:
java.lang.String[].new [3,3]
CHAPTER 6 ?–  JAVA INTEGRATION 104
or
java.lang.String[3][3].new
or
java.lang.String[3,3].new
To work with such an array, just do it the normal way:
d = java.lang.String[3,3].new
d[0][0] = "Hello"
d[0][1] = "World"
Working with arrays in this way works fine with the primitive types too:
Java::byte[256].new
If you need to reference the Java classes for arrays, you use the same form:
java.


Pages:
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190