为什么 Scala 中的映射函数不读取数组 [英] Why map function in scala not reading Arrays

查看:31
本文介绍了为什么 Scala 中的映射函数不读取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Scala 编程很陌生,因此提出这个问题

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

我的Scala程序在下面

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)
}


}

为什么我得到的输出是 [I@14991ad

Why I am getting output as [I@14991ad

我希望读取数组元素,并希望每个元素乘以 10 并打印结果

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

我也想知道scala中map()的返回类型是什么

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

推荐答案

Array 是 Scala 表示 Java 数组的一种方式.调用 Array.toString() 方法(您在 println(k) 中隐式执行)调用 Java 的 Object<中定义的 toString 方法/code> 类(默认打印对象哈希码的十六进制表示).您应该尝试使用 Scala 的 List 类型,它覆盖了 toString() 方法并输出列表的漂亮内容.

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)`

您还可以轻松地将 Array 转换为 List:

You can also easily convert an Array to a List:

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

对于你的第二个问题:映射一个 Array 返回另一个 Array.

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

这篇关于为什么 Scala 中的映射函数不读取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆