1:实现两个数组对象当中在不引入第三个变量的时候进行数值的交换操作
2:寻找出两个数组当中所共有的重复元素
3:对一个数组对象实现向右移动n位
/** * * @author Administrator *1:实现数组对象当中任意两个位置处对象元素的交换操作 *2:找出两个数组对象当中的重复元素对象 *3:对一个数组对象实现移位功能 */ public class test10 { /* * 对数组当中的任意两个位置的数字进行变量的交换且不适用第三个变量 * a=10 * b=20 * * &a=a+b;(a+b然后再将自身减去就剩下b,从而实现变量的交换操作) * &b=A(a+b)-b=a; * &a=&a(a+b)-&b(a)=b * * * &a=b; * &b=a; * * */ public static void method1(int[] arrays,int p1,int p2) { arrays[p1]=arrays[p1]+arrays[p2]; arrays[p2]=arrays[p1]-arrays[p2]; arrays[p1]=(arrays[p1]-arrays[p2]); System.out.println("对指定位置的两个元素进行交换之后"); for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i]+" "); } System.out.println(); } /*用于去除两个字符数组对象当中的重复字符对象将之组成一个新的字符数组对象 * 1.将两个重复字符数组当中的重复字符去除 * 2.然后将两个字符数组进行排序 * 3.将两个字符数组当中相同的数组对象提取出来组成一个新的数组 * */ public static void method2(char[] c1,char[] c2) { String s1=new String(c1); String s2=new String(c2); /* * String s2=c2.toString(); * toString方法默认的返回值为 return this; * 所以s2所获取到的是字符数组c2的地址值 * */ StringBuffer stringBuffer=new StringBuffer(); c1=noRepeat(s1); c2=noRepeat(s2); /*System.out.println("method2"); System.out.println(new String(c1)); System.out.println(new String(c2));*/ s1=new String(c1); s2=new String(c2); int j; for (int i = 0; i <s1.length(); i++) { for (j = 0; j <s2.length(); j++) { if(s1.charAt(i)==s2.charAt(j)) { stringBuffer.append(s1.charAt(i)); break; } } } String strings=stringBuffer.toString(); System.out.println("数组当中的重复元素对象为:"); System.out.println(strings); } public static char[] noRepeat(String str) { // System.out.println("方法调用"); StringBuffer stringBuffer=new StringBuffer(); while(str.length()!=0) { // System.out.println(str.length()); char c=str.charAt(0); stringBuffer.append(c); String s=""; str=str.replace(""+c,""); } // System.out.println("对缓存打印输出"+stringBuffer); String str1=stringBuffer.toString(); char[] c=str1.toCharArray(); return c; } /** * 进行数组对象的循环位移操作, * 当位移的长度和数组的长度相同的时候不进行移动,当小于要进行移动的次数的时候 * 先将最左边的变量进行存放然后将剩余数组当中的变量值都向左进行移动操作, * 将最右边所空出的位置用于进行第一个临时变量值的存放操作 * 按上述方法来进行循环操作即可(在进行移动的时候会有越界异常) */ public static void method3(int arrays[],int num) { int i,temp; while(num>0) { temp=arrays[0]; for(i=1;i<arrays.length;i++) { arrays[i-1]=arrays[i]; } arrays[i-1]=temp; num--; } for (i = 0; i < arrays.length; i++) { System.out.print(arrays[i]+" "); } } public static void main(String[] args) { int[] arrays=new int[]{1,2,3,4}; method1(arrays, 2, 3); int[] arrays3=new int[]{1,2,3,4}; method3(arrays3,2); /*调试代码 * char[] c=noRepeat("dasfjklasjdfa"); for (int i = 0; i < c.length; i++) { System.out.println(c[i]); }*/ // method2(new char[]{'q','q','c','d'}, new char[]{'b','b','m','n'}); char[] c1=new char[]{'q','q','c','d'}; char[] c2=new char[]{'d','b','m','n'}; method2(c1, c2); } }