How to check the existence of a value in an ArrayBuffer?

谁说我不能喝 提交于 2019-12-24 02:25:56

问题


I am new in Scala and I am writing a program in which I have an ArrayBuffer of points of a binary image and I want to check in a loop if a specific point is existing in that ArrayBuffer do not add. This is the part of code I am working on :

var vectVisitedPoint= new scala.collection.mutable.ArrayBuffer[Point]()
    var pTemp=new Point (0,0)
    var res = new Array[Byte](1)
    img.get(pTemp.x.toInt,pTemp.y.toInt,res) //img is a binary image
    var value1: Int=0
    var value2: Int=0
    scala.util.control.Breaks.breakable {
            while((value1 < img.rows ) ){
                    while ( (value2 < img.cols )){
                             if (res(0) == -1 && vectVisitedPoint.exists(value1,value2)) {//this is where I want to check if the current point (value1,value2) is already exists in vectVisitedPoint
                                    pTemp.x=(pTemp.x.toInt)+value1
                                    pTemp.y=(pTemp.y.toInt)+value2
                                    vectVisitedPoint.append(new Point(pTemp.x,pTemp.y)
                                    scala.util.control.Breaks.break()
                              }
                    value2=value2+1
                    img.get(value1,value2,res)
                    }
            value2=0
            value1=value1+1
            }
    }
}

I think I need to write it in another way but don't know how?!

Thanks.


回答1:


You can use:

vectVisitedPoint.exists(_ == (value1, value2))

Would you like me to refactor your code for you into much much less code, more functional, more readible and probably more efficient way? If so create another question and I will.



来源:https://stackoverflow.com/questions/25705715/how-to-check-the-existence-of-a-value-in-an-arraybuffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!