Program to print Java List of characters in Scala
Last Updated :
29 Nov, 2019
A java list of characters can be returned from a Scala program by writing a user defined method of Java in Scala. Here, we don’t even need to import any Scala’s JavaConversions object in order to make this conversions work.
Now, lets see some examples.
Example:1#
object GfG
{
def main(args : Array[String])
{
def result = {
val list = new java.util.ArrayList[Char]()
list.add( 'g' )
list.add( 'f' )
list.add( 'g' )
println(list)
}
val list = result
}
}
|
Therefore, a list of characters is returned from a Java method. Here, we don’t need to import any object of Scala. In the above program a Java method is written in Scala program. Where, this method adds the characters elements of the list to the stated list one after another and then prints the results.
Example:2#
object GfG
{
def main(args : Array[String])
{
def result = {
val list = new java.util.ArrayList[Char]()
list.add( 'l' )
list.add( 'm' )
list.add( 'n' )
println(list)
}
val list = result
}
}
|
It is same as above example but here one more element is added in the stated list and here proper order of the elements is not required.
Share your thoughts in the comments
Please Login to comment...