1. {1,2,3} → {3,2,1} 노가다
package algo;
public class ChangeData {
public static void main(String[] args) {
// 배열 만들어서 순서 바꾸기
// {1,2,3} -> {3,2,1} reverse
int[] arr = {1,2,3};
int temp = arr[2]; // 2. arr 2번지에 있는 3을 temp에 보관
arr[2] = arr[0]; // 1. 이렇게만 하면 {1,2,1}이 되서 3이 날라가기 때문에 작업하기 전에 3을 다른 곳에 보관해야 함
arr[0] = temp; // 3. temp에 있던 3이 arr 0번지에 보관되서 {3,2,1}이 됨
for (int i = 0; i < arr.length; i++) { // arr배열의 길이
System.out.println(arr[i]);
}
}
}
2. {1,2,3,4,5,6,7} → {7,6,5,4,3,2,1} 노가다
package algo;
public class ChangeDataMe21 {
public static void main(String[] args) {
// {1,2,3,4,5} -> {5,4,3,2,1} reverse
int[] arr = {1,2,3,4,5};
int temp1 = arr[4];
arr[4] = arr[0];
arr[0] = temp1;
int temp2 = arr[3];
arr[3] = arr[1];
arr[1] = temp2;
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
3. 1~10 → 10~1
1) 노가다
package algo;
public class ChangeDataMe23 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10};
int temp;
temp = arr[9];
arr[9] = arr[0];
arr[0] = temp;
temp = arr[8];
arr[8] = arr[1];
arr[1] = temp;
temp = arr[7];
arr[7] = arr[2];
arr[2] = temp;
temp = arr[6];
arr[6] = arr[3];
arr[3] = temp;
temp = arr[5];
arr[5] = arr[4];
arr[4] = temp;
for (int i = 0; i < 10; i++) {
System.out.println(arr[i]);
}
}
}
2) 변수 정리
package algo;
public class ChangeDataMe231 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10};
int temp;
int x = 9;
int y = 0;
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
x--;
y++;
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
x--;
y++;
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
x--;
y++;
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
x--;
y++;
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
x--;
y++;
for (int i = 0; i < 10; i++) {
System.out.println(arr[i]);
}
}
}3) for문
package algo;
public class ChangeDataMe232 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10};
int temp;
int x = 9;
int y = 0;
for (int i = 0; i < 5; i++) {
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
x--;
y++;
}
for (int i = 0; i < 10; i++) {
System.out.println(arr[i]);
}
}
}4) 최종 정리
package algo;
public class ChangeDataMe232 {
public static void main(String[] args) {
// {1,2,3,4,5,6,7,8,9,10} -> {10,9,8,7,6,5,4,3,2,1} reverse
int[] arr = {1,2,3,4,5,6,7,8,9,10}; // 입력값. 배열 입력
int temp;
int z = arr.length/2; // 고정값. 치환 횟수
int x = arr.length-1; // 고정값. arr 마지막 번지 수
int y = 0; // 고정값. arr 첫 번째 번지 수
for (int i = 0; i < z; i++) {
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
x--;
y++;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
+) 가로 나열
package algo;
public class ChangeDataMe232 {
public static void main(String[] args) {
// {1,2,3,4,5,6,7,8,9,10} -> {10,9,8,7,6,5,4,3,2,1} reverse
int[] arr = {1,2,3,4,5,6,7,8,9,10}; // 입력값. 배열 입력
int temp;
int z = arr.length/2; // 고정값. 치환 횟수
int x = arr.length-1; // 고정값. arr 마지막 번지 수
int y = 0; // 고정값. arr 첫 번째 번지 수
for (int i = 0; i < z; i++) {
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
x--;
y++;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
Share article