scala 如何初始化元组列表并在scala中添加项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29142321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to init List of tuple and add item in scala
提问by whb
why this is NOT valid?
为什么这是无效的?
var a = List[(String,String)]
a = a :: ("x","y")
basically i want to init a list of tuple and add a tuple to the list.
基本上我想初始化一个元组列表并将一个元组添加到列表中。
回答by eliasah
Here you have two mistakes.
这里你有两个错误。
The first one is that you are trying to instantiate List which an abstract class
第一个是你试图实例化一个抽象类的 List
I believe that what you are trying to do is the following
我相信你正在尝试做的是以下
var a : List[(String,String)] = List()
This will create a list of an empty list of tuples.
这将创建一个空的元组列表。
The second is that you are trying to add an element which is not actually a tuple so I believe that you should try the following
第二个是您正在尝试添加一个实际上不是元组的元素,所以我相信您应该尝试以下操作
a = a:+(("x","y"))
Here you are defining a tuples and adding it to your List a
在这里,您正在定义一个元组并将其添加到您的列表中
回答by Ben Reich
You can instantiate an empty List[(String, String)]in many ways:
您可以通过List[(String, String)]多种方式实例化一个空:
val list = List[(String, String)]()
val list = List.empty[(String, String)]
val list : List[(String, String)] = Nil
val list : List[(String, String)] = List()
val list = Nil : List[(String, String)]
val list : List[(String, String)] = List.empty
Note, too, that the default instantiations of Traversable, Iterable, Seq, and LinearSeqall also return a List, so you can use those too, (e.g. Seq.empty[(String, String)]).
注意,那就是,默认实例Traversable,Iterable,Seq,和LinearSeq所有也返回List,这样你就可以使用这些呢,(例如Seq.empty[(String, String)])。
And then you can add elements using :+:
然后您可以使用:+以下方法添加元素:
val newList = list :+ ("x", "y")
val newList = list :+ ("x" -> "y")
You were using the cons method ::, which is used for prepending. In Scala, methods that end in :are right associative:
您正在使用 cons 方法::,该方法用于前置。在 Scala 中,以 结尾的方法:是右结合的:
val newList = ("x" -> "y") :: list
You can also call them using the regular dot syntax if you want them to be left associative, like normal methods:
如果您希望它们保持关联,您也可以使用常规点语法调用它们,就像普通方法一样:
val newList = list.::("x" -> "y")
The reason this method is right associative is because it prepends elements, allowing you to do things like:
这个方法是右关联的原因是因为它在元素之前,允许你做这样的事情:
val newList = ("x" -> "y") :: ("a" -> "b") :: list
and retain the expected order (the same order that it appears in the code).
并保留预期的顺序(与代码中出现的顺序相同)。
回答by Thronghar
You could also write the add like this if you want to add to an existing Array :
如果您想添加到现有 Array ,您也可以像这样编写 add :
var a : List[(String,String)] = List()
a:+=(("x","y"))
回答by surfealokesea
Simple straight way in declaration:
声明中的简单直接方式:
val l=List("type"->"number","min"->"2","step"->"1","value"->"2")

![scala 添加两个 RDD[mllib.linalg.Vector]'s](/res/img/loading.gif)