Subato

MoreSimpleArrayFuns

public final class MoreSimpleArrayFuns {

  private MoreSimpleArrayFuns() {
  }

  static int firstIndex(int[] xs,int y){
    for (int i = 0;i<xs.length;i++)if (xs[i]==y) return i;
    return -1;
  }
  static boolean startsWith(int[] prefix, int[] xs){
    if (prefix.length>xs.length) return false; 
    for (int i = 0;i<prefix.length;i++)if (xs[i]!=prefix[i]) return false;
    return true;
  }
  static void replace(int[] xs, int oldI, int newI){
    for (int i = 0;i<xs.length;i++)
      if (xs[i]==oldI)
        xs[i] = newI;
  }
  static void reverse(int[] xs){
    for (int i = 0;i<xs.length/2;i++){
      var x = xs[i];
      xs[i] = xs[xs.length-1-i];
      xs[xs.length-1-i] = x;
    }    
  }
  static int[] getSubArray(int[] xs, int startIndex, int length){
    var l = startIndex+length>xs.length?xs.length-startIndex:length;
    var result = new int[l];
    for (int i = 0;i<result.length;i++){
      result[i] = xs[startIndex+i];
    }
    
    return result;
  }
}