import java.util.*; int[] A = {
12, 13, 34, 62, 24, 12};
int[] B = {
34, 62, 24, 13, 23, 52, 62, 24};
int[] temp = new int[A.length + B.length];
int[] temp1 = new int[A.length + B.length];
System.arraycopy(A, 0, temp, 0, A.length);
System.arraycopy(B, 0, temp, A.length, B.length);
Arrays.sort(temp);
int length = -1, tempt = -1;
for (int i = 0; i < temp.length; i++) {
if (length == -1) {
temp1[i] = temp[i];
tempt = temp[i];
length = 1;
}
else {
if (tempt == temp[i])
continue;
temp1[length++] = temp[i];
tempt = temp[i];
}
}
if ( length == -1 )
System.exit( 0 );
int[] C = new int[ length ];
System.arraycopy( temp1, 0, C, 0, length );算法我不保证最有,但绝对有效
12, 13, 34, 62, 24, 12};
int[] B = {
34, 62, 24, 13, 23, 52, 62, 24};
int[] temp = new int[A.length + B.length];
int[] temp1 = new int[A.length + B.length];
System.arraycopy(A, 0, temp, 0, A.length);
System.arraycopy(B, 0, temp, A.length, B.length);
Arrays.sort(temp);
int length = -1, tempt = -1;
for (int i = 0; i < temp.length; i++) {
if (length == -1) {
temp1[i] = temp[i];
tempt = temp[i];
length = 1;
}
else {
if (tempt == temp[i])
continue;
temp1[length++] = temp[i];
tempt = temp[i];
}
}
if ( length == -1 )
System.exit( 0 );
int[] C = new int[ length ];
System.arraycopy( temp1, 0, C, 0, length );算法我不保证最有,但绝对有效
public static void main(String[] args){
int[] intArrayA={12,13,34,62,24};
int[] intArrayB={34,62,24,13,23,52,62,24};
ArrayList listA=new ArrayList();
ArrayList listB=new ArrayList();
for(int i=0;i<intArrayA.length;i++){
listA.add(new Integer(intArrayA[i]));
}
for(int i=0;i<intArrayB.length;i++){
listB.add(new Integer(intArrayB[i]));
}
TreeSet hsetA=new TreeSet(listA);
TreeSet hsetB=new TreeSet(listB);
hsetA.addAll(hsetB);
Object[] result=hsetA.toArray();
int[] resultIntArray=new int[result.length];
for(int i=0;i<result.length;i++){
resultIntArray[i]=((Integer)result[i]).intValue();
}
for(int i=0;i<resultIntArray.length;i++){
System.out.println(resultIntArray[i]);
}
}
}
实现了SortedSet 接口, 另外, Set 是不可以含有重复元素的.
所以, hsetA.addAll(hsetB) 就是把2个Set合并, 并区除重复元素.
import java.util.*;
class join{
static int temp=0;
static int[] a={12,13,34,62,24,24};
static int[] b={34,62,24,13,23,52,62,24};
static int[] d=new int[a.length+b.length];
static void create(){
System.arraycopy(a,0,d,0,a.length);
System.arraycopy(b,0,d,a.length,b.length);
}
static void printlnarray(int[] t){
for(int n=0;n<t.length;n++)
System.out.println(t[n]);
}
public static void main(String[] args){
create();
for(int h=0;h<d.length-1;h++)
for(int k=h+1;k<d.length;k++)
if ((d[h]==d[k])&&(d[h]!=0))
{
d[k]=0;
temp++;
}
Arrays.sort(d);
//System.out.println(d.length);
//System.out.println(temp);
int[] c=new int[d.length-temp];
System.arraycopy(d,temp,c,0,d.length-temp);
//printlnarray(a);
//printlnarray(b);
printlnarray(c);
//printlnarray(d);
}
}
所以,用JAVA时,别太在意性能,用一门语言就是要用它的长处.
以上的3个代码片那个最易理解,那个最易读,我想你自己会有
个判断.
因为题目是这样的。
我会写一个更通用的程序和大家分享。
tiger999(不吃肉的老虎)说的也有道理,不过题目是要最有效的方法和代码
我以为是性能所以就朝那方面努力了。
你的程序给了我很大启发。
我的改进程序,希望没有令大家失望。import java.util.*;
class joinpro{
static int j,i;
static int[] a={-23,0,12,-6,13,34,62,24,24};
static int[] b={34,-56,62,0,-23,24,-6,13,23,52,62,24};
static int[] d=new int[a.length+b.length];
static int[] e=new int[a.length+b.length];
static void create(){
System.arraycopy(a,0,d,0,a.length);
System.arraycopy(b,0,d,a.length,b.length);
}
static void printlnarray(int[] t){
for(int k=0;k<t.length;k++)
System.out.print(t[k]+" ");
System.out.println();
}
public static void main(String[] args){
create();
Arrays.sort(d);
e[j++]=d[i++];
do{
if(d[i]!=d[i-1]){
e[j]=d[i];
j++;
}
i++;
}while(i<d.length);
int[]c=new int[j];
System.arraycopy(e,0,c,0,j);
printlnarray(a);
printlnarray(b);
printlnarray(d);
printlnarray(c);
}
}//-23 0 12 -6 13 34 62 24 24
//34 -56 62 0 -23 24 -6 13 23 52 62 24
//-56 -23 -23 -6 -6 0 0 12 13 13 23 24 24 24 24 34 34 52 62 62 62
//-56 -23 -6 0 12 13 23 24 34 52 62