Arrays.toString() / Arrays.deepToString()

2023. 4. 21. 00:12

💡 Arrays.toString() <- java.util.Arrays

➡️ 배열의 요소를 문자열로 변환하여 반환하는 메소드

// 1차원 배열의 경우
int[] arr = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(arr)); // -> [1, 2, 3, 4, 5]
// 2차원 배열인 경우
int[][] arr = {{1,2,3,}, {4,5,6}};
System.out.println(Arrays.toString(arr)); //
// 배열 안에 또다른 배열을 가지고 있기 때문에 내부 배열의 첫 번째 주소값들을 차례로 출력
// 만약 2차원 배열의 모든 요소를 출력하고 싶다면
int[][] arr = {{1,2,3,}, {4,5,6}};
  System.out.println(Arrays.deepToString(arr)); // [[1, 2, 3], [4, 5, 6]]

 

 

'Java > Method' 카테고리의 다른 글

str.replace(char oldChar, char newChar)  (0) 2023.04.22
Character.getNumericValue()  (0) 2023.04.22
length / length()  (0) 2023.04.20
Math.floor() / Math.floorDiv()  (0) 2023.04.17
forDigit() 메서드  (0) 2023.04.17

BELATED ARTICLES

more