Scala复杂类型

时间:2016-05-22 15:13:38

标签: arrays scala

我与Scala合作。我定义了以下函数

scala> def getEdgesMortages( fila: Array[((org.apache.spark.graphx.VertexId,
ComplaintNodeDate),(org.apache.spark.graphx.VertexId, MortageNode))]): 
Array[String] = {
 | println("row ",fila)
 | println("row 0 ",fila(0))
 | var (idVertice, datoComplaint) = fila(0)
 | println("idVertice ",idVertice)
 | var z = Array("z")
 | return z
 | }
getEdgesMortages: (fila: Array[((org.apache.spark.graphx.VertexId,  
ComplaintNodeDate), (org.apache.spark.graphx.VertexId,
MortageNode))])Array[String]

我用值

执行了这个函数
 scala>getEdgesMortages(Array(((101,ComplaintLoan("01/22/2014","Mortgage","Conventional 
fixed mortgage",
"Web","Closed with explanation")),
(501,MortageData("Refinancing","Application denied by financial 
institution",
"One-to-four family dwelling (other than manufactured housing)")))))

退出消息

(row ,[Lscala.Tuple2;@29f6dc76)
(row 0 ,((101,ComplaintLoan(01/22/2014,Mortgage,Conventional fixed   
mortgage,Web,Closed with explanation)),
(501,MortageData(Refinancing,Application denied by financial    
institution,One-to-four family dwelling (other than manufactured 
 housing)))))
(idVertice ,(101,ComplaintLoan(01/22/2014,Mortgage,Conventional fixed 
mortgage,Web,Closed with explanation)))
res55: Array[String] = Array(z)

我必须提取

的值101
((101,ComplaintLoan(01/22/2014,Mortgage,Conventional fixed    
  mortgage,Web,Closed with explanation)),

这是数组fila的索引为0的元素。

如何提取值101?

1 个答案:

答案 0 :(得分:0)

您可以使用_<idx>访问元组值,其中idx是一个基础。 e.g:

scala> val t = (1,2,3)
t: (Int, Int, Int) = (1,2,3)

scala> t._1
res0: Int = 1

所以在你的情况下element._1._1之类的东西应该有用。

请记住,从函数返回元组通常被认为是不好的做法,您是否考虑过创建临时case class