과거⚰️

배열 기본 테스트 1 2

아무루 2020. 11. 14. 10:56
package Day036;

public class Ex00501배열테스트1문제 {
    public static void main(String[] args) {

        int arr1[] = {10,20,30,40,50};
        int b[] = {10,20,3,5};

        //문제) arr 안에 b의 값이 있으면 0으로 변경
        // 예) arr = {0,0,30,40,50};

        int x = 0;
        for(int i=0; i<arr1.length; i++){
            if(arr1[i]==b[x]){
                arr1[i]=0;
                x++;
            }
        }
        for(int i=0; i<arr1.length; i++){
            System.out.print(arr1[i]+" ");
        }
        System.out.println();
        for(int i=0; i<b.length; i++){
            System.out.print(b[i]+" ");
        }
    }
}


package Day036;

public class Ex00501배열테스트2문제 {
    public static void main(String[] args) {

        int arr[][] = {
                {10,20,30},
                {40,50,60},
                {70,80,90},
        };
        int temp [] = {10,2,54,90,50};
        // temp 에 arr의 값이 있으면 0으로 변경
        //예) temp ==> {0,2,54,0,0}
           int y = 0;
           int x = 0;
           for(int i=0; i<9; i++) {
               for (int j = 0; j < temp.length; j++) {
                   if (arr[y][x] == temp[j]) {
                      temp[j]=0;
                   }
               }
              x++;
               if(x==3){
                   x=0;
                   y++;
               }
               if(y==3){
                   break;
               }
           }
        for(int i=0; i<temp.length; i++){
            System.out.print(temp[i]+" ");
        }
    }
}