[英]Scala - How to extract list from java.io.Serializable type
I want to pass List along with other string parameters as argument.我想将 List 与其他字符串参数一起作为参数传递。 When I am trying to retrieve the list in called function it gives type Mismatch error.
当我试图检索名为 function 的列表时,它会给出类型不匹配错误。
val list = ("abc","def","xyz")
val ex_args = Array("--arg1","arg2",list)
lst_args = ex_args.toList
// Get back the list in other function
val mylist:List[String] = lst_args(2) <------ This fails with type mismatch error
ERROR:
type mismatch;
found : java.io.Serializable
required: List[String]
Okay, this will be difficult and painful for you, but I hope it will give you motivation to start learning Scala.好的,这对您来说将是困难和痛苦的,但我希望它能给您提供开始学习 Scala 的动力。 First of all, you are not creating a list, but a tuple with 3 elements, you can read what is a tuple here .
首先,您创建的不是一个列表,而是一个包含 3 个元素的元组,您可以在此处阅读什么是元组。 To create a list you must use the List keyword (
val list = List("abc", "def", "xyz")
), you can read how to do that here .要创建列表,您必须使用 List 关键字(
val list = List("abc", "def", "xyz")
),您可以在此处阅读如何执行此操作。 Secondly, when you use List(index) you will get an element of your list, not a list, in your example you will get a String, but not a list String.其次,当您使用 List(index) 时,您将获得列表的一个元素,而不是列表,在您的示例中,您将获得一个字符串,但不是列表字符串。 I suggest you read this book and practice a little.
我建议你读这本书并练习一下。
The code itself for your question:您的问题的代码本身:
val list = List("abc", "def", "xyz")
val ex_args = mutable.ArrayBuffer("--arg1", "arg2").addAll(list)
val lst_args = ex_args
val myList: List[String] = lst_args.toList
val singleElement = myList(2)
println(singleElement)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.