每个整数只有最低8个有效位用来存储数据,所以提取整数到检测数组之前需要先和0xFF做与运算来提取该整数的后8位。
然后就是通过移位运算判断编码是否有效,用Arrays.CopyOfRange检测余下的数组编码是否有效。
要点:!!!因为if判断里会用到d[0],d[1],d[2],d[3]移位的操作,所以最前面一定要加d.length够不够取出d[1]、d[2]、d[3]这样的判断,不然你可能会花很长时间去找 咦,我到底哪里越界了呢????

  import java.util.Arrays;
  import java.util.Scanner;


  public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
        String[] str=sc.next().split(",");
        short[] d=new short[str.length];
        for(int i=0;i<str.length;i++){
            int n=Integer.parseInt(str[i]);
            d[i]=(short)(n&0xFF);
            }
         System.out.println(inUtf8(d));       
    }
   }
    public static boolean inUtf8(short[] d) {
        if (((d[0]>>7)==0)){
            if(d.length==1){
                return true;
            }
            else return inUtf8(Arrays.copyOfRange(d, 1, d.length));

        }
        else if (d.length>=2&&((d[0]>>5)==6)&&((d[1]>>6)==2)){
            if(d.length==2){
                return true;
            }
            else 
                return inUtf8(Arrays.copyOfRange(d, 2, d.length));
        }
        else if (d.length>=3&&((d[0]>>4)==14)&&((d[1]>>6)==2)&&((d[2]>>6)==2)){
            if(d.length==3){
                return true;
            }
            else 
                return inUtf8(Arrays.copyOfRange(d, 3, d.length));

        }else if (d.length>=4&&((d[0]>>3)==30)&&((d[1]>>6)==2)&&((d[2]>>6)==2)&&((d[3]>>6)==2)){
            if(d.length==4){
                return true;
            }
            else 
                return inUtf8(Arrays.copyOfRange(d, 4, d.length));

        }else return false;
    }
  }