62,612
社区成员
发帖
与我相关
我的任务
分享
public class SearchSame {
public static void main(String[] args) {
int ary[]={-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
boolean a[]=new boolean[ary.length];
for(int i=0;i<ary.length;i++){
a[i]=true;
}
for(int i=0;i<ary.length-1;i++){
if(ary[i]==ary[i+1])a[i+1]=false;
}
int k=0;
for(int i=0;i<a.length;i++){
if(a[i]==true)k++;
}
int newary[]=new int[k];
int h; int l=0;
for( h=0;h<ary.length;h++){
if(a[h]==true){
newary[l]=ary[h];
l++;
}
}
for(int i=0;i<newary.length;i++){
System.out.println(newary[i]);
}
}
}
int[] a = {-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9,9};
int count = 0;
for(int i=0;;i++) {
try {
if (a[i] != a[i+1]) {count++;}
} catch(Exception e) {
break;
}
}
int[] b = new int[count+1];
count = 0;
for(int i=1;;i++) {
try {
if (a[i] != a[i+1]) {b[count++]=a[i];}
} catch(Exception e) {
b[count] = a[i];
break;
}
}
System.out.println(Arrays.toString(b));
int[] a = {-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
boolean[] bs = new boolean[a.length];
for (int i=0; i<a.length; i++)
bs[i] = (i==0 || a[i]!=a[i-1])? true : false;
int id=0,count=0;
for (int i=0; i<bs.length; i++)
if (bs[i]) count++;
int[] result = new int[count];
for (int i=0; i<bs.length; i++)
if (bs[i]) {
result[id] = a[i];
id++;
}
for (int i=0; i<result.length; i++)
System.out.print(result[i] + " ");