2011-04-28 114 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; 

    }