2015-02-02 2 views
-1

Im Ardunio를 사용하지만 Java의 솔루션이 만족 스럽다면 이해할 수 있습니다.배열에서 연속 된 중복 값 제거 - Arduino/Java

나는 길이 (50)의 배열 stateX[]을 가지고 있고 그것은 유사한 값을 개최한다 : 나는 다음에있는 배열에서 중복 된 값을 제거 할 수있는 방법을 알고

0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, -1, -1, 0, 1, 1...... 
내가 좋아하는 것

을 서로. 따라서 출력은 요소가있는 더 작은 배열이됩니다.

0, 1, 0, -1, 0, 1 

분명히이 작은 배열의 크기가 변경되므로 분명히 알 수 있습니다. 반복되는 값을 감지하는 방법도 있습니다.

도움말 감사, 감사

+1

물어 hesistate이 도움이 될 수 그나마 희망, 단순히 루프를 사용합니까? – jhamon

+0

은 다음과 같이 인덱스를 사용하여 배열을 반복하는 데 루프를 사용하고 다음 값이 현재와 동일한 동안 증가시킵니다 (최종 배열에 저장할 수 있음) –

답변

0

당신이 그것을 할 수있는 "체계적인"방법을 알고 아니지만, 여기에서 방법 :

int[] array = new int[]{1,0,0,1,1,1,-1,-1,1,-1}; 
List<Integer> lst = new ArrayList<Integer>(); 

lst.add(array[0]); 

for(int i=1;i<array.length;i++) 
{ 
    if(array[i]!=lst.get(lst.size()-1)) lst.add(array[i]); 
} 

int lstSize = lst.size(); 
int[] revisedArray = new int[lstSize]; 
for(int i = 0; i < lstSize; i++) revisedArray[i] = lst.get(i); 
0

다음 솔루션은 현재 위치에서와 아무튼 '를 작동 자바 목록을 사용하지 마십시오.이 두 특성은 Arduino/C로 이식하는 데 도움이됩니다. O (a.length)의 복잡성이 있습니다.

입출력 배열의 크기가 조정되지 않고 값이 제거되면 너무 커지게됩니다. 반환 값 newSize은 솔루션 길이를 나타냅니다. 즉, 메소드의 첫 번째 newSize 값만이 호출 된 후 관련됩니다.

public static int removeAdjacentDuplicates(int[] a) { 
    int newSize = 1; 
    for (int i = 1; i < a.length; i++) { 
     // check if the current value differs from the preceding one 
     if (a[i] != a[i - 1]) { 
      a[newSize++] = a[i]; 
     } 
    } 
    return newSize; 
} 
0

미안 해요, 난 Ardunio 몰라,하지만 난 자바에 다음과 같은 클래스를 만들어, 꽤 자기 설명은 질문! :

package test; 

import java.util.ArrayList; 
import java.util.Random; 

public class Duplicates { 

    static ArrayList<Integer> ints = new ArrayList<Integer>(); 

    public static void main(String[] args) 
    { 

     //Creating an arrayList with random numbers from 0 to 5, note that it will work with any numbers. 
     for(int index = 0 ; index <= 1000 ; index++) 
     { 
      Random myRandom = new Random(); 
      int x = (int) (myRandom.nextDouble() * 5); 
      ints.add(x); 

     } 

     //Calling my method to remove dupliates next to another 
     ArrayList<Integer> cleanedList = removeNextToAnotherDuplicates(ints); 

     //Displaying it in the console 
     System.out.println(cleanedList.toString()); 

    } 

    public static ArrayList<Integer> removeNextToAnotherDuplicates(ArrayList<Integer> list) 
    { 

     for(int i = 0 ; i < list.size() ; i++) 
     { 
      boolean continueFlag = true; 

      // j = j because I do not want to always increment my position, since I would skip some due to the fact that I remove duplicates 
      for(int j = i + 1; j < list.size() && continueFlag ; j = j) 
      { 
       //Showing each iterations. 
       System.out.println("i("+ i +") :" + list.get(i).intValue() + " j("+ j +") :" + list.get(j).intValue()); 

       if(list.get(i).intValue() == list.get(j).intValue()) 
       { 
        list.remove(j); 
       } 
       else 
       { 
        continueFlag = false; 
        j++; 
       } 

       //Showing each iterations. 
       System.out.println("flag: "+ continueFlag); 
       System.out.println(list.toString()); 

      } 
     } 

     return list; 
    } 
}