728x90

 

 

 

https://fall-in-dream.tistory.com/16

 

반복문 없이 배열 출력하기 ( Arrays.toString(arr))- JAVA

보통 배열을 출력하려고 하면 반복문을 사용하죠? 반복문을 사용한 출력 class Solution { public static void main(String args[]) throws Exception { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8}; // 배열 선언 for(int i = 0; i < arr.length

fall-in-dream.tistory.com

 

 

 

 

1차원 배열은 해결 완료!!!

 

그런데

2차원 배열을 출력할 때 Arrays.toString(arr); 을 사용하면 어떻게 될까요?

class Solution
{
	static Scanner sc = new Scanner(System.in);
	public static void main(String args[]) throws Exception
	{
		System.out.print(Arrays.toString(arr_test()));
	}
    
    public static int[][] arr_test()
    {
    	int[][] arr = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    	return arr;
    }
}

 

결과물

 

[[I@74a14482, [I@1540e19d, [I@677327b6, [I@14ae5a5]

 

전에 함수에서 리턴한 배열을 그냥 출력하려고 할 때 나왔던 애들이 이제는 4개나!!!!

 

그러면 어떡해야 하느냐

 

Arrays.deepToString(출력하고 싶은 배열);

(참고, import java.util.Arrays; 필요합니다.)

 

class Solution
{
	static Scanner sc = new Scanner(System.in);
	public static void main(String args[]) throws Exception
	{
		System.out.print(Arrays.deepToString(arr_test()));
	}
    
    public static int[][] arr_test()
    {
    	int[][] arr = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    	return arr;
    }
}

 

 

결과물

[[1, 2], [3, 4], [5, 6], [7, 8]]

 

글자가 조금 달라지자

이렇게 잘 출력되는 모습을 볼 수 있습니다.

2차원 배열 뿐만 아니라 다차원 배열도 사용 가능하니 편하게 사용해보세요!!

 

 

다음에 또 봐요

 

728x90

보통 배열을 출력하려고 하면 반복문을 사용하죠?

 

반복문을 사용한 출력

class Solution
{	
	public static void main(String args[]) throws Exception
	{
		int[] arr = {1, 2, 3, 4, 5, 6, 7, 8}; // 배열 선언
		for(int i = 0; i < arr.length; i++)
		{
			System.out.print(arr[i]+" ");
		}
	}
}

 

결과물

1 2 3 4 5 6 7 8

 

이렇게 할거라고 생각합니다.

근데 혹시 함수에서 return 값으로 배열을 받아올 때, 이걸 어떡하지 생각한 적 있지 않으신가요?

 

 

함수에서 배열을 return 받았을 때

class Solution
{
	static Scanner sc = new Scanner(System.in);
	public static void main(String args[]) throws Exception
	{
		System.out.print(arr_test);
	}
    
    public static int[] arr_test()
    {
    	int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
    	return arr;
    }
}

 

결과물

[I@74a14482

 

이 코드를 실행하면 이렇게 이상한게 나온다.

배열(arr)이 가지고 있는 값이 아닌, 그 값을 갖고 있는 주소값을 출력하게 되기 때문!!!!

 

그러면 어떡해야 할까?

Arrays.toString(출력하고 싶은 배열의 이름);

(참고. import java.util.Arrays 해줘야 사용 가능합니다.)

class Solution
{
	static Scanner sc = new Scanner(System.in);
	public static void main(String args[]) throws Exception
	{
		System.out.print(Arrays.toString(arr_test()));
	}
    
    public static int[] arr_test()
    {
    	int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
    	return arr;
    }    
}

 

그러면 

[1, 2, 3, 4, 5, 6, 7, 8]

 

짜잔~~~ 하고 배열이 모두 출력된다.

 

하지만 이 방법으로는 2차원 배열은 출력할 수 없습니다!!!!!

 

그러면 어떡해야하냐.............는 다음 시간에 올리도록 하겠습니다. ㅎㅎ

 

어떻게 해야하는지 밑에 올려놨습니다.

https://fall-in-dream.tistory.com/17

 

반복문 없이 2차원(다차원) 배열 출력하기 ( Arrays.deepToString(arr))- JAVA

https://fall-in-dream.tistory.com/16 반복문 없이 배열 출력하기 ( Arrays.toString(arr))- JAVA 보통 배열을 출력하려고 하면 반복문을 사용하죠? 반복문을 사용한 출력 class Solution { public static void main(String args[])

fall-in-dream.tistory.com

 

 

다음에 또 봐요

+ Recent posts