타닥타닥 개발자의 일상

배열의 순서 변수 생성해서 바꾸기 바꾸기, 함수 이용하여 배열 순서 바꾸기 본문

코딩 기록/Java

배열의 순서 변수 생성해서 바꾸기 바꾸기, 함수 이용하여 배열 순서 바꾸기

NomadHaven 2022. 1. 24. 21:21

temp를 기억하자

package study0124_2;

public class Main {

	public static void main(String[] args) {

		int[] question1 = {10,20};
		System.out.println(question1[0]+" "+question1[1]);
		swap(question1);
		System.out.println(question1[0]+" "+question1[1]);

	}
	
	public static void swap(int []A) {
			int temp = 0;
			temp = A[0]; // temp에 A[0] 값을 집어넣는다
			A[0] = A[1]; // A[0]에 A[1] 값을 집어넣는다.
			A[1] = temp;//  A[1]에 temp값(=A[0]값)을 집어 넣는다.
			
			
		
		}
		
	}
Comments