Reputation: 169
I have a matrix in terms of a 2D array and another 1-D array. I am taking one element from the matrix and checking whether it exists in the array. Following is the code.
val array_intrval = Array.ofDim[Int](10)
var joint_matrix = Array.ofDim[Int][Int](5)(2)
for(i <- 0 to 4) {
for (j <- 0 to 1) {
var a = joint_matrix(i)(j)
After this, I want to check whether a exists in array_intrval, if not add a in array_intrval and then check whether there are some elements which is less than or equal to a. If yes, also put them in array_intrval. If a does exists in array_intrval, skip a and check for next element in joint_matrix.
I am a beginner in Scala and not able perform this. Any help regarding this will be highly appreciated.
Upvotes: 3
Views: 7672
Reputation: 13056
Take a look at the documentation of Array class. You'll find many useful methods there. For ex., a method called contains
can be used to check if a certain element exists in the array or not.
scala> val array_intrval = Array.ofDim[Int](10)
array_intrval: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> array_intrval(1) = 2
scala> array_intrval
res1: Array[Int] = Array(0, 2, 0, 0, 0, 0, 0, 0, 0, 0)
scala> array_intrval.contains(2)
res3: Boolean = true
scala> array_intrval.contains(0)
res4: Boolean = true
scala> array_intrval.contains(5)
res5: Boolean = false
Upvotes: 4