java简单数据压缩代码
public static int[] compress(int[] tmp) {StringBuffer buffer = new StringBuffer();int begin = -1;//记录重复数据从哪里开始的for(int i =0;i < tmp.length;i++){if(i !=0 && tmp[i] != tmp[i-1])//与上一个元素比较begin = -1;//与下一个元素比较if((i+1)!= tmp.length && tmp[i] == tmp[i+1]){boolean flag = false;//重复开始索引if(begin == -1){begin = i;flag = true;buffer.append(0);buffer.append(",");}if(i !=0 && tmp[i] != tmp[i-1]&&!flag){buffer.append(i-begin-1);buffer.append(",");begin = -1;}}else if(-1 != begin&&(i+1)== tmp.length){//最后一个元素buffer.append(i-begin+1);}else if(i !=0 && (i+1)!= tmp.length && tmp[i] == tmp[i-1] && tmp[i] != tmp[i+1]){buffer.append(i-begin+1);buffer.append(",");} //不需要压缩的数据if( begin == -1){buffer.append(tmp[i]);buffer.append(",");}}String[] tmpS = buffer.toString().split(",");int[] retArray = new int[tmpS.length];for(int i = 0 ;i< tmpS.length;i++){retArray[i] = Integer.parseInt(tmpS[i]);}return retArray;}?