2017-01-01 2 views
-3

이 질문을하지 말아야한다는 것을 알고 있지만, 자바에서 내 프로그램을위한 약간의 알고리즘을 개발하는 데 도움이 필요합니다. 내가 배열의이 종류가 : 여기에 문제가있어알고리즘에 대한 도움이 필요합니다.

// 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 루프를 다시 수행하여) ...하지만 ' 더 쉽고/더 나은 방법을 찾고 있습니다. (기본 배열은 매우 길며, 기술이 많이 최적화되지 않은 것 같습니다.)

감사합니다.

+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

그리고 당신은 자바 스트림을 사용하려는 경우, 아래 당신이 할 수있는 것입니다.

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

관련 문제