2011-04-28 39 views
2

我的代码在java中2个数组的联合?

class Union { 

    //Search Function 
    static boolean search(int A[], int i) { 

     for (int k = 0; k < A.length; k++) { 
      if (A[k] == i) { 
       return true; 
      } 
     } 
     return false; 
    } 

    //union 
    static void union(int A[][], int B[][]) { 

     int i = 0; 
     int count = 0; 
     int C[] = new int[A.length + B.length]; 

     for (; i < A.length; i++) { 
      if (!(search(B, A[i]))) { 
       C[count] = A[i]; 
       count++; 
      } 
     } 

     for (; i < (A.length + B.length); i++) { 
      C[count] = B[i - A.length]; 
      count++; 
     } 

     System.out.println("This is Union Of 2 D Array "); 
     System.out.println(); 

     for (int k = 0; k < count; k++) { 
      System.out.println(C[k]); 
     } 
     System.out.println(); 

    } 

    public static void main(String... s) { 
     union(new int[]{1, 1, 1, 4,}, new int[]{1, 4, 4, 4, 1, 2}); 
    } 
} 

我使用这个输出找到二维数组的工会。但这些我得到的输出是错误的。我不想要2在java中使用任何预定义的接口和方法。 我的答案应该是 {1,2,4}

A= {1,2,3,3} 
B={2,3,1,1} 
c={1,2,3} 
+1

您的代码似乎只有一维数组。二维数组在哪里? – 2011-04-28 11:51:36

+0

如果我在这段代码中错了,请更正我 – 2012-03-30 02:00:24

+1

@Guarav_Java:当我们不知道你在找什么时很难纠正你 - 但是彼得是对的,你*只是使用一维数组。请注意,数组类型变量的惯用声明是将所有类型信息保存在一起:'int [] x'而不是'int x []'。此外,参数名称通常是camelCased。 – 2012-03-30 05:14:59

回答

9

这是你在找什么:

import java.util.Arrays; 

public class Union 
{ 

    public static void main(String[] args) 
    { 
     int[] A = {1, 2, 3, 3}; 
     int[] B = {2, 3, 1, 1}; 
     System.out.println(Arrays.toString(unionArrays(A, B))); 
    } 

    /* Union of multiple arrays */ 
    public static int[] unionArrays(int[]... arrays) 
    { 
     int maxSize = 0; 
     int counter = 0; 

     for(int[] array : arrays) maxSize += array.length; 
     int[] accumulator = new int[maxSize]; 

     for(int[] array : arrays) 
      for(int i : array) 
       if(!isDuplicated(accumulator, counter, i)) 
        accumulator[counter++] = i; 

     int[] result = new int[counter]; 
     for(int i = 0; i < counter; i++) result[i] = accumulator[i]; 

     return result; 
    } 

    public static boolean isDuplicated(int[] array, int counter, int value) 
    { 
     for(int i = 0; i < counter; i++) if(array[i] == value) return true; 
     return false; 
    } 
} 

OUTPUT:

[1, 2, 3] 
+0

感谢您的好解答 – 2012-03-31 03:56:31

+0

int [] A = {4,5,6}; \t \t int [] B = {1,2,3,5,1,1};它在这个输入 – 2016-06-19 03:55:50

8

未进行具体回答你的问题,但如果你真的只是想获得一个联盟,你应该使用java Set接口。详情请参阅here

1

你发布的代码是处理1d数组,而不是2d =)代码似乎试图将两个数组的内容连接到另一个数组中。对于这一点,只是以下:

public static int[] joinArrays(int[] a, int[] b) { 
    if (a == null || b == null) 
     throw new IllegalArgumentException("Both arrays must be non-null"); 
    int c[] = new int[a.length + b.length]; 
    System.arraycopy(a, 0, c, 0, a.length); 
    System.arraycopy(b, 0, c, a.length, b.length); 
    return c; 
} 
+0

我想这样运行,而不使用System.arraycopy – 2011-04-28 12:02:27

1

A = {1,1,1,4-} B = {1,4,4,4,1,2}

数学集合A的联合和B将是C = {1,4,2}

还是你想重复,如C = {1,1,1,1,1,2,4,4,4,4}

哪一个是您的预期产出?第一个还是第二个?

public class Union_2{ 
static int size; 

public static void main(String [] args){ 
int [] a = {1,1,1,4}; 
int [] b = {1,4,4,4,1,2}; 
int [] c = Union_finder(a,b); 
for(int i = 0 ; i< size ; i++){ 
    System.out.print(c[i]+" "); 
} 
} 
public static int[] Union_finder(int [] a,int [] b){ 
int [] c = new int[a.length+b.length]; 
int i=0,j=0,k=0; 
for(;i<a.length;i++){ 
    boolean bool = check(a[i],c); 
    if(bool == false){ 
    c[k] = a[i]; 
    size++; 
    k++; 
    } 
} 
for(;j<b.length;j++){ 
    boolean bool = check(b[j],c); 
    if(bool== false){ 
    c[k] = b[j]; 
    size++; 
    k++; 
    } 
} 
return c ; 
} 
public static boolean check(int x,int [] c){ 
if(size == 0){ 
    return false; 
} 
else{ 
    for(int i = size - 1 ; i >= 0 ; i--){ 
    if(c[i] == x){ 
     return true ; 
    } 
    } 
} 
return false ; 
} 
} 
+0

第一个输出 这个u回答可以在评论中也可以做 – 2012-03-30 11:05:03

+0

我添加了你正在寻找的代码,它有助于。 – rumman0786 2012-03-30 14:28:50

4

集是一个自然的选择,当你想唯一性。为了避免大量转换,您可以将int[]更改为Integer[],并获得一个非常简洁的联合方法。

下面是一个完整的工作示例:

import java.util.*; 

public class Union { 
    // Search Function 
    public boolean search(Integer a[], Integer i) { 
    for(int k = 0; k < a.length; k++) { 
     if(a[k] == i) { 
     return true; 
     } 
    } 
    return false;    
    } 

    // Union 
    public void union(Integer[] a, Integer[] b) { 
    Set<Integer> set = new HashSet<Integer>(Arrays.asList(a)); 
    set.addAll(Arrays.asList(b)); 
    Integer[] unionArray = set.toArray(new Integer[set.size()]); 
    System.out.println(Arrays.toString(unionArray)); 
    } 

    public static void main(String...s) { 
    Integer[] array1 = new Integer[]{1,1,1,4,}; 
    Integer[] array2 = new Integer[]{1,4,4,4,1,2}; 
    new Union().union(array1, array2); 
    } 
} 

很明显,有一定的开销这里从数组转换为列表,然后该列表设置,那么重新设置为阵列。但是,通常不值得使用更复杂的代码来实现更快的速度 - 只有当您发现代码中的这部分代码存在性能瓶颈时,才能使用直接和更长的代码解决方案。

使用Set还避免了一个常见的错误,即遍历数组搜索元素以确认要添加的元素不重复。通常,诸如此类的解决方案具有O(n^2)时间复杂性(参见this)。

这不会是一个问题,当你的数组有10个元素,但如果你有两个数组,每个1000个独特的元素,你会做很多不必要的步骤,让你的代码真的很慢。在这种情况下,在基于阵列的解决方案中通过遍历阵列进行重复检查时,必须执行1000 * 1000/2 = 500K操作,而基于集合的操作将接近5k:

  • 1000向第一阵列转换到一个列表,
  • 1000转换列表来设置,
  • 1000向第二阵列转换到一个列表,
  • 1000向第二阵列添加到该集合和
  • 1000将其从集合中转换回来)

作为基于集合的解决方案是O(n)。如果你假设这些操作大致相同(不是真的,但不是很差的近似值),这个速度要快100倍。

此外,随着独特元素数量的增加,这种增加速度很快 - 对于每个阵列中的10K个元素,基于阵列的步行解决方案需要50,000,000次操作的订单,而基于集合的步骤将采用15,000。

希望这会有所帮助。

+0

@icyrocks失败。很好的答案,谢谢你的帮助。但我限制不使用这些函数和预定义的方法 – 2012-03-31 03:53:59

+0

@icyrocks。感谢这个答案 – 2012-03-31 03:58:12

0

//我希望这个例子很简单。 //传递两个数组,你一定会得到没有重复项的数组的UNION。

 
public class UnionOfArrays 
{

public int[] getUnion(int[] arr1, int[] arr2) { int[] array = MergeSort.mergeArray(arr1, arr2); int[] arrReturn = getunique(array); return arrReturn; } public int[] getunique(int[] array) { int[] arrTemp = new int[array.length]; int[] arrReturn; int index = 0; for (int i = 0; i < array.length; i++) { Boolean found = false; for (int j = 0; j < i; j++) { if (array[i] == array[j]) { found = true; break; } } if (!found) { arrTemp[index++] = array[i]; } } arrReturn = new int[index]; for (int i = 0; i < index; i++) { arrReturn[i] = arrTemp[i]; } return arrReturn; }}
0
public static int[] arrayUnion(int a1[], int a2[]){ 
     int[] resultArray={}; 
     ArrayList<Integer> arrayList = new ArrayList<Integer>(); 

     if(a1.length>a2.length){ 
      resultArray=new int[a1.length]; 
     }else resultArray=new int[a2.length]; 

     for(int element : a1){ 
      arrayList.add(Integer.valueOf(element)); 

     } 
     for(int element:a2){ 
      if(! arrayList.contains(element)){ 
       arrayList.add(Integer.valueOf(element)); 
      } 
     } 

     resultArray = arrayList.stream().mapToInt(i->i).toArray(); // only in java 8 
     return resultArray; 

    }