ArrayList example in JRuby

In this section of JRuby tutorial you will learn how to create and add elements in ArrayList using the JRuby.

ArrayList example in JRuby

ArrayList example in JRuby

     

In this section of JRuby tutorial you will learn how to create and add elements in ArrayList using the JRuby. ArrayList is used to store elements in the list which can be accessed sequentially.

In this example of ArrayList we have to include the ArrayList class as we import class file in simple java program. After including ArrayList we have created a new instance of ArrayList so that we can call add method of this class to add elements in it.

 

 

ArrayList.rb

require "java"
include_class "java.util.ArrayList"
  arrlist = ArrayList.new
  arrlist.add "Amit"
  arrlist.add "Kumar"
  arrlist.add "Vineet"
puts "#{arrlist}"

First line in ArrayList.rb file require "java" means that we are making our program enabled to use java classes and method. "puts" method prints array list on the console.

Output: 


C:\JRuby>jruby ArrayList.rb
[Amit, Kumar, Vineet]

C:\JRuby>

Download Source Code