0

I don't know how convert java "continue" to scala. I can use marker from bool + break, but its bad idea Google did not help :( It's my first program in scala... yep.. it's horrible

sort java

def sort(in: Array[Int], a:Int, b:Int)
  {
     var i,j,mode;
     double sr=0;
     if (a>=b) return;                                     // size =0
     for (i=a; i<=b; i++) sr+=in[i];
     sr=sr/(b-a+1);
     for (i=a, j=b; i <= j;)
            {
            if (in[i]< sr) { i++; continue; }        // left > continue
            if (in[j]>=sr) { j--; continue; }         // right, continue
            int c = in[i]; in[i] = in[j]; in[j]=c;
            i++,j--;                                       // swap and continue
            }
     if (i==a) return;
     sort(in,a,j); sort(in,i,b);
  }

sort scala...

def SortMerger(in:List[Int], a:Int, b:Int):Unit = {
    var i = 0;
    var j = 0;
    var mode = 0;
    var sr = 0.0;
    if(a>=b) return;
    i=a
    while(i<=b)
    {
      sr+=in.ElementOf(i);
      i += 1
    }
    sr=sr/(b-a+1)
    i=a
    j=b
    while(i<=j)
    {
      if(in.ElementOf(i)<sr) {
        i += 1; 
        // where continue??? ><
        }
    }

    return
  }

1 Answer 1

2

Scala has no continue statement, but what you are trying to do can be done with a simple if/else structure.

while(i<=j) 
{
  if(in(i) < sr) {
   i += 1
  } else if (in(j) >= sr) {
    j -= 1
  } else {
    int c = in(i)
    in(i) = in(j)
    in(j) = c
    i += 1
    j -= 1
  }
}

Note that the type of in here should be Array, not List

Sign up to request clarification or add additional context in comments.

2 Comments

List is my class single-connected list))
@StriBog Ah I see. Using the same name as a standard library class can lead to some confusion. I still say you should use an array though. a linked-list is terribly inefficient for this kind of work compared to an Array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.