1.两个char数组一个,判断一个是否包含另一个char数组,比如 char[] a={'a','b','c','d','g'},char[] b={'c','d'},char[] c={'f','b'},则a包含b,a不包含c
public class includeChar{
public static void main(String args[]){
char Xchar[]={'a','b','c','b','g','c','b','z'};
char Ychar[]={'c','b','b'};
int alength,blength,flaga=-1,flagb=-1;
char[] achar;
char[] bchar;
boolean mark=false;
if(Xchar.length>=Ychar.length){
achar=Xchar;
bchar=Ychar;
}else{
bchar=Xchar;
achar=Ychar;
}
alength=achar.length;
blength=bchar.length;
for(int i=0;i<blength;i++){
for(int j=0;j<alength;j++){
if( achar[j]==bchar[i]){
flaga=j;
flagb=i;
mark=true;
break;
}
}
if(mark){
break;
}
if(i==blength-1){
System.out.println("这两个数组没有包含关系");
}
}
if(flaga!=-1&&flagb!=-1){
if((flaga-flagb+blength)>alength){
System.out.println("这两个数组没有包含关系");
}else{
for(int i=1;i<blength-flagb;i++){
if(achar[flaga+i]!=bchar[flagb+i]){
System.out.println("这两个数组没有包含关系");
break;
}
if(i==blength-1){
System.out.println("这两个数组存在包含关系");
}
}
}
}
}
}
2.找到char数组中首个不重复的字符,比如char[] a={'a','a','b','c'},则找出来的应该是b
public class repeatChar{
public static void main(String args[]){
char []reChar={'1','1','2','2','2','3'};
boolean flag=false;
for(int i=1;i<reChar.length;i++){
if(reChar[0]!=reChar[1]){
System.out.println("此数组中首个不重复的字符是:"+reChar[0]);
break;
}
if(i>1&&i<reChar.length-1){
if(reChar[i]!=reChar[i+1]&&reChar[i-1]!=reChar[i]){
System.out.println("此数组中首个不重复的字符是:"+reChar[i]);
break;
}
}
if(i==reChar.length-1){
if(reChar[reChar.length-1]!=reChar[reChar.length-2]){
System.out.println("此数组中首个不重复的字符是:"+reChar[reChar.length-1]);
break;
}else{
System.out.println("此数组中没有不重复的字符");
}
}
}
}
}