2014-11-14 9 views
0

Java에서 2 차원 배열이 있습니다. 값은 다음과 같습니다.2 차원 Java 배열에서 검색 및 표시

2 1 0 2 1 
1 0 2 2 2 
1 1 1 2 2 
0 0 0 2 2 

이제 모든 "2"를 1로 변경하려고하지만 다른 하나와 경계를 맞추는 사람 만 변경하고 싶습니다. ? 당신은이 작업 가정이 일을하는 가장 빠른 방법은 무엇

2 1 0 1 1 
1 0 1 1 1 
1 1 1 1 1 
0 0 0 1 1 

+0

"다른 것들과 접하는 것"이란 무엇을 의미합니까? –

+0

내 예제에서 알 수 있습니다. 2 at (0 | 0)은 변경되지 않아야합니다. 다른 2s 옆에있는 사람들. –

+0

그래서 2가 다른 쪽 2와 "쪽"을 공유한다면 –

답변

0

: | 그래서 말을 한 후 "변경 (4 4)"2 "는,이처럼 내 배열을 갖고 싶어 차원 배열 :

have int x and y set to 0 
start from [x][y] on the array 
Step 1: Check to if number equals "2" 
    If it does and: 
    0<x<4 and 0<y<4: Check [x-1][y-1],[x][y-1],[x][y+1],[x-1][y],[x+1][y], and [x+1][y+1] 
    x=0 and 0<y<3: Check [x][y-1],[x][y+1],[x+1][y], and [x+1][y+1] 
    x=4 and 0<y<3: Check [x-1][y-1],[x][y-1],[x][y+1],[x-1][y] 
    0<x<4 and y=0: Check [x][y+1],[x-1][y],[x+1][y], and [x+1][y+1] 
    0<x<4 and y=3: Check [x-1][y-1],[x][y-1],[x-1][y],[x+1][y] 
    and so on.... 
check to see if [x][y] = any of the checks 
    if so: store a 1 in [x][y] in an alternative array of the same size (lets call it flags) 
iterate and repeat from step 1 until you have gone through the entire array 
run through the flags and if the value is 1 at any address [x][y] change the corresponding value in our original array to "1". 

죄송는 조금 말의 아마 혼란, 내가

지금까지 시도했다 당신의 코드
0

를 명확히해야하는 경우 알려 주시기 바랍니다 당신은 루프 중첩을 사용하여 시도 할 수 6,

는 다음이 옆에 있으면
1. 인덱스
2의 왼쪽과 오른쪽에있는 값을 볼 것이라는 내부 루프의 경우-else 문을 넣어 2 (그리고 2)는 인덱스 w/a 1에서 2를 바꾸거나 할당합니다. 그렇지 않으면 그냥 혼자 남습니다.

0s, 1s 및 2s의 배열이나 사용자가 입력 한 배열과 함께 사용할 수 있도록이 확장 가능 제품을 만들려고하십니까?

명확한 설명이 필요하면 알려 주시고 코드를 포함 시키면 효과가있을 수 있습니다.

0

이 예제는 2D 배열을 배열의 배열로 사용합니다.

  1. 그것은 난수 2 차원 배열을 생성

    • 배열 [] [] 소스 배열 모든 여분의 루틴의 복사본이다 (교체 [] [] 어레이에서 수행 될 것이다 소스 1).
    • SIZE_I - 행 수 및 SIZE_J - 열 수.
    • RANGE_FROM - 배열의 최소 수와 RANGE_TO - 최대 수.
    • 검색 - 검색 번호;
    • REPLACE - 대체 할 숫자입니다.
    • 수량 - 개수 또는 발견되고 대체 된 품목.
  2. 원본 배열을 화면에 인쇄합니다.

  3. 검색 상수와 배열 항목 배열 [i] [j]가 일치하는 원본 배열을 확인합니다. 항목의 위치에 따라, 대체 된 [] [] 배열에서이 항목을 대체하고 수량 변수를 하나씩 늘리려면 여러 조건이 충족되어야합니다.

  4. 화면에 대체 된 배열과 대체 된 항목의 수를 인쇄합니다.

저는 2D 배열을 하나의 배열로 사용하여 성능을 향상시킬 수 있다고 생각합니다. 예를 들어, 2D 배열은 {2,1,0211022211122000,2,2 } 제대로 처리해야합니다.

public static void main(String[] args) { 
final int RANGE_FROM = 0; 
final int RANGE_TO = 2; 
final int SEARCH = 2; 
final int REPLACE_TO = 1; 
final int SIZE_I = 4; 
final int SIZE_J = 5; 
int quantity = 0; 
int array[][] = new int[SIZE_I][SIZE_J]; 
int replaced[][] = new int[SIZE_I][SIZE_J]; 

// Generate arrays 
for (int i = 0; i < SIZE_I; i++) { 
    for (int j = 0; j < SIZE_J; j++) { 
    array[i][j] = (int) (RANGE_FROM + Math.random() * (RANGE_TO - RANGE_FROM + 1)); 
    replaced[i][j] = array[i][j]; 
    } 
} 

// Display array 
System.out.println("Source array:"); 
for (int x[]: array) { 
    for (int y: x) { 
    System.out.print(y + " "); 
    } 
    System.out.println(); 
} 
System.out.println(); 

// Check array 
for (int i = 0; i < array.length; i++) { 
    for (int j = 0; j < array[i].length; j++) { 
    if (i == 0 && j == 0) { 
     if (array[i][j] == SEARCH && (array[i+1][j] == SEARCH || array[i][j+1] == SEARCH)) { 
     quantity++; 
     replaced[i][j] = REPLACE_TO; 
     } 
    } 
    else if (i == 0 && j == array[i].length - 1) { 
     if (array[i][j] == SEARCH && (array[i+1][j] == SEARCH || array[i][j-1] == SEARCH)) { 
     quantity++; 
     replaced[i][j] = REPLACE_TO; 
     } 
    } 
    else if (i == array.length -1 && j == 0) { 
     if (array[i][j] == SEARCH && (array[i-1][j] == SEARCH || array[i][j+1] == SEARCH)) { 
     quantity++; 
     replaced[i][j] = REPLACE_TO; 
     } 
    } 
    else if (i == array.length -1 && j == array[i].length - 1) { 
     if (array[i][j] == SEARCH && (array[i][j-1] == SEARCH || array[i-1][j] == SEARCH)) { 
     quantity++; 
     replaced[i][j] = REPLACE_TO; 
     } 
    } 
    else if (i == 0 && j != 0 && j != array[i].length - 1) { 
     if (array[i][j] == SEARCH && (array[i][j-1] == SEARCH || array[i+1][j] == SEARCH || array[i][j+1] == SEARCH)) { 
     quantity++; 
     replaced[i][j] = REPLACE_TO; 
     } 
    } 
    else if (i != 0 && i != array.length - 1 && j == array[i].length - 1) { 
     if (array[i][j] == SEARCH && (array[i][j-1] == SEARCH || array[i-1][j] == SEARCH || array[i+1][j] == SEARCH)) { 
     quantity++; 
     replaced[i][j] = REPLACE_TO; 
     } 
    } 
    else if (i == array.length - 1 && j != 0 && j != array[i].length - 1) { 
     if (array[i][j] == SEARCH && (array[i][j-1] == SEARCH || array[i-1][j] == SEARCH || array[i][j+1] == SEARCH)) { 
     quantity++; 
     replaced[i][j] = REPLACE_TO; 
     } 
    } 
    else if (i != 0 && i != array.length - 1 && j == 0) { 
     if (array[i][j] == SEARCH && (array[i-1][j] == SEARCH || array[i][j+1] == SEARCH || array[i+1][j] == SEARCH)) { 
     quantity++; 
     replaced[i][j] = REPLACE_TO; 
     } 
    } 
    else { 
     if (array[i][j] == SEARCH && (array[i-1][j] == SEARCH || array[i][j+1] == SEARCH || array[i+1][j] == SEARCH || array[i][j-1] == SEARCH)) { 
     quantity++; 
     replaced[i][j] = REPLACE_TO; 
     } 
    } 
    }  
} 

// Display replaced array 
System.out.println("Replaced array:"); 
for (int x[]: replaced) { 
    for (int y: x) { 
    System.out.print(y + " "); 
    } 
    System.out.println(); 
} 
System.out.println(); 
System.out.println("Replace quantity: " + quantity); 
System.out.println(); 
} 
+0

답안을 좀 더 자세하게 설명해 주시겠습니까? 그러면 코드가하는 일이 명확 해 집니까? – avalancha

관련 문제