1

I am very new to scala programming , hence asking this question

My scala program is below

object FirstMain{

 def main(args: Array[String]): Unit={

 val myArray = Array(1,2,3,4)
 val k =myArray.map(x => x*10)
 println(k)
}


}

Why I am getting output as [I@14991ad

I want the array elements to be read and want each element to be multiplied with 10 and print the result

Also I would like to know what is the return type of map() in scala

Surender Raja
  • 3,553
  • 8
  • 44
  • 80
  • 1
    `[I@14991ad` *is* that new array. Arrays don't print their elements like `List`s do. Try using `List(1,2,3,4)` instead of `Array(1,2,3,4)`, especially if you are a beginner and don't know completely understand the difference yet. – Bergi Jun 03 '16 at 11:28
  • yes got it .. tried printing like k(0), K(1).. it prints.. also tried with List(1,2,3,4) it prints all in one print statement. Thanks for your help – Surender Raja Jun 03 '16 at 11:32
  • 1
    On the other question of the reutrn type for "map()" in scala: You might be interested in reading [How are Scala collections able to return the correct collection type from a map operation?](http://stackoverflow.com/questions/5200505/how-are-scala-collections-able-to-return-the-correct-collection-type-from-a-map) and more specifically w.r.t. Arrays: [Scala Doc of Collections - Arrays](http://docs.scala-lang.org/overviews/collections/arrays) – Dilettant Jun 03 '16 at 11:36
  • if you just want to see the result and you don't want to use List, do `println(k.mkString(", "))`. It will convert your entries in the array to string and will therefore be easier to see when you are using println. – jtitusj Jun 03 '16 at 12:01

3 Answers3

2

Array.map will return another Array

println(k) <=> println(String.valueOf(k)) <=> println(k.toString)

the Array does not override the method of toString if you want to print your array like this Array(10, 20, 30, 40), you can use the following code

println(k.deep)

deep not in Scala 2.13 see SO answer about Array deep documentation.

Good luck with you

CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
Jerry
  • 492
  • 4
  • 8
2

map builds a new collection by applying a function to all elements of this array.

To print an Array you can do the following :

val myArray = Array(1, 2, 3, 4)
// myArray: Array[Int] = Array(1, 2, 3, 4)

val k = myArray.map(x => x * 10)
// k: Array[Int] = Array(10, 20, 30, 40)

println(k.mkString("[", ",", "]"))
// [10,20,30,40]

Hence, you can read here what does actually [I@14991ad means. (Array of Integer with hashcode of the Array object 14991ad)

Community
  • 1
  • 1
eliasah
  • 39,588
  • 11
  • 124
  • 154
2

Array is a Scala's way of representing Java arrays. Invoking Array.toString() method (which you do implicitly in println(k)) invokes toString method defined in Java's Object class (which by default prints hexadecimal representation of the hash code of the object). You should try using Scala's List type, which overrides toString() method and outputs pretty contents of the list.

val list = List(1, 2, 3, 4)
println(list) // Prints: `List(1, 2, 3, 4)`

You can also easily convert an Array to a List:

val list = Array(1, 2, 3, 4).toList

For your second question: mapping an Array returns another Array.

Paweł Jurczenko
  • 4,431
  • 2
  • 20
  • 24