2017-01-01 101 views
-3

我知道我不應該問這個問題,但我真的需要幫助爲我的程序在java中開發一個小算法。 這裏的問題: 我有這類型的數組:需要算法的幫助

// note that {1, 1} is present twice, is duplicated 
int[][] array = {{0, 1}, {0, 2}, {1, 1}, {3, 5}, {1, 1}, {2, 2}}; 

我想擺脫這2個不同的數組:

int[][] norepetition = {{0,1},{0,2},{3,5},{2,2}}; 
int[][] withrepetition = {{1,1}}; 

功能應單獨初始陣列分爲2個新的陣列,其中一個包含不重複的座標,另一個包含多次出現的座標。

我想過使用for循環並遍歷每個座標並將其複製到一個新表中A檢查是否已經有相同的座標(通過再次執行for循環)......但是, m尋找一個更容易/更好的方式(基陣非常長,恐怕我的技術沒有太多優化)。

謝謝!

+0

java哪個版本? – Sebas

+2

你可以改善你的問題標題嗎?乍一看,它會讓你的問題更容易理解。 – byxor

+4

如果你不關心元素的順序,你應該看'Set'和'HashSet'。 – byxor

回答

-1
import java.util.*; 

public class StackQ2 { 

    static class IntContainer { 
     public IntContainer(int a, int b) { 
      this.a = a; 
      this.b = b; 
     } 
     int a; 
     int b; 

     @Override 
     public boolean equals(Object o) { 
      if (this == o) return true; 
      if (o == null || getClass() != o.getClass()) 
       return false; 

      IntContainer that = (IntContainer) o; 

      if (a != that.a) return false; 
      return b == that.b; 

     } 

     @Override 
     public int hashCode() { 
      int result = a; 
      result = 31 * result + b; 
      return result; 
     } 

     @Override 
     public String toString() { 
      return "{" + a + "," + b + "}"; 
     } 
    } 
    public static void main(String[] args) { 

     List<IntContainer> array = Arrays.asList(
       new IntContainer(0, 1), 
       new IntContainer(0, 2), 
       new IntContainer(1, 1), 
       new IntContainer(3, 5), 
       new IntContainer(1, 1), 
       new IntContainer(2, 2) 
     ); 
     List<IntContainer> norepetition = new ArrayList<>(); 
     Set<IntContainer> withrepetition = new HashSet<>(); 
     for (IntContainer element : array) { 
      if (Collections.frequency(array, element) > 1) { 
       withrepetition.add(element); 
      } else { 
       norepetition.add(element); 
      } 
     } 

     System.out.println("NoRep: " +Arrays.toString(norepetition.toArray())); 
     System.out.println("Rep: " +Arrays.toString(withrepetition.toArray())); 

    } 

輸出

NoRep: [{0,1}, {0,2}, {3,5}, {2,2}] 
Rep: [{1,1}] 
+0

非常感謝你的代碼人! – Vellyxenya

0

如果你想使用Java流,下面是你可以做什麼。

import java.util.List; 
import java.util.stream.Collectors; 
import java.util.stream.Stream; 

public class JavaStreams { 

    public static void main(String argv[]) 
    { 
     Stream<Integer[]> stream = Stream.of(new Integer[][]{{0, 1}, {0, 2}, {1, 1}, {3, 5}, {1, 1}, {2, 2}}); 
     List<Integer[]> matching = stream.filter(i -> i[0] == i[1]).collect(Collectors.toList()); 
     Stream<Integer[]> notmatchingstream = Stream.of(new Integer[][]{{0, 1}, {0, 2}, {1, 1}, {3, 5}, {1, 1}, {2, 2}}); 
     List<Integer[]> notmatching = notmatchingstream.filter(i -> i[0] != i[1]).collect(Collectors.toList()); 

     System.out.println("Matching Integer arrays are: "); 
     matching.stream().forEach(p -> System.out.println(p[0]+", "+p[1])) ; 
     System.out.println("Not Matching Integer arrays are: "); 
     notmatching.stream().forEach(p -> System.out.println(p[0]+", "+p[1])) ; 

    } 

} 
+0

將輸入從'int [] []'改變爲'Integer [] []'可能不合適。 – Andreas