class Solution {
public int[][] solution(int[] num_list, int n) {
int[][] answer = new int[num_list.length / n][n]; // [num_list.length / n] -> 행의 갯수 계산 / num_list.length는 num_list 배열의 길이 / n은 열의 개수
// 2차원 배열 생성
for (int i = 0; i < num_list.length / n; i++) { // n = 2 경우, 총 4 번돌고
// 외부 반복문 i는 행을 표현
for (int j = 0; j < n; j++) { // n = 2 경우, 총 8 번돈다
// 내부 반복문 j는 열을 표현
answer[i][j] = num_list[i*n+j]; // n가 행의 위치를 나타냄
// [j+i*n]
// [0*2+0 = 0] = 1
// [0*2+1 = 1] = 2
// [1*2+0 = 2] = 3
// [1*2+1 = 3] = 4
// [2*2+0 = 4] = 5
// [2*2+1 = 5] = 6
// [3*2+0 = 6] = 7
// [3*2+1 = 7] = 8
// 각 2줄씩 = 열 / 총 4줄 = 행 / 배열(총 개수)은 8
// [0*3+0 = 0] = 1
// [0*3+1 = 1] = 2
// [0*3+2 = 2] = 3
// [1*3+0 = 3] = 4
// [1*3+1 = 4] = 5
// [1*3+2 = 5] = 6
// [2*3+0 = 6] = 7
// [2*3+1 = 7] = 8
// [2*3+2 = 8] = 9
// 각 3줄씩 = 열 / 총 3줄 = 행 / 배열(총 개수)은 9
}
}
return answer;
}
}
answer 배열 생성num_list 배열의 길이를 n으로 나눈 값으로 행의 개수를 계산num_list.length / n 개의 행과 n 개의 열um_list.length는 num_list 배열의 길이n은 열의 개수num_list.length / n은 행의 개수answer 배열에 값을 할당i) : 행j) : 열num_list.length / n을 계산하면 행의 개수
answer 배열은 8을 2로 나눈 값인 3개의 행
최종적으로 num_list 배열이 2개의 열을 가진 2차원 배열로 변환
[[1, 2],
[3, 4],
[5, 6],
[7, 8]]
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]@Transactional
public ProductResponseDto updateProduct(Long id, ProductMypriceRequestDto requestDto) {
int myprice = requestDto.getMyprice();
if(myprice < MIN_MY_PRICE) {
throw new IllegalArgumentException("유효하지 않은 관심 과격입니다. 최소 " + MIN_MY_PRICE + "원 이상으로 설정해주세요.");
}
Product product = productRepository.findById(id).orElseThrow(() -> new NullPointerException("해당 상품을 찾을 수 없습니다."));
product.update(requestDto);
return new ProductResponseDto(product);