2017-01-01 10 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つの新しい配列に初期配列を区切る必要があります1つは繰り返されない座標を含み、もう1つは複数回来る座標を含む。

私はfor-loopを使って考えて、各座標を通り、すでに同じ座標があるかどうかをチェックした後に新しいテーブルAにコピーします(for-loopをやり直すことによって)...しかし、より簡単で良い方法を探しています(ベースアレイは非常に長く、私の技術はあまり最適化されていないのではないかと思います)。

ありがとうございました!

+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