반응형
- C0907이차원메모리구조
- C0907개념01_이차원메모리구조
- 변수 개수를 셀 수 있어야 한다.
package C0907이차원메모리구조;
public class C0907개념01_이차원메모리구조 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 30;
int[] d = {10,20,30};
int[] t1 = {1,2,3};
int[] t2 = {4,5,6};
int[] t3 = {7,8,9};
int[][] t4 = new int[3][];
t4[0] = t1;
t4[1] = t2;
t4[2] = t3;
//------------------------------------------------
int[][] arr = new int[3][3]; // 변수 13개 그려져야된다.
System.out.println(arr[0]); // [I@7852e922
arr[0][0] = 10;
arr[0][1] = 20;
arr[0][2] = 30;
System.out.println(arr[1]); // [I@4e25154f
arr[1][0] = 40;
arr[1][1] = 50;
arr[1][2] = 60;
System.out.println(arr[2]); // [I@70dea4e
arr[2][0] = 70;
arr[2][1] = 80;
arr[2][2] = 90;
System.out.println(arr);
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println("-------------------------------");
int num = 10;
int[] test = null;
int[] temp = arr[1];
for(int i=0; i<temp.length; i++) {
System.out.print(temp[i] + " ");
}
}
}
- 메모리
- int [ ] [ ] arr = new int [3][3]; => 13개가 만들어 진다.
- int [ ] [ ] t4 = new int [3] [ ] ; => 4개
- 머리까지만 만들어 진다.
- int [ ] [ ] arr = new int [3][3]; => 13개가 만들어 진다.


- int a = 10;
int b = 20;
int c = 30;
int[] d = {10,20,30};
int[] t1 = {1,2,3};
int[] t2 = {4,5,6};
int[] t3 = {7,8,9};
int[][] t4 = new int[3][];
t4[0] = t1;
t4[1] = t2;
t4[2] = t3;

- int [ ] [ ] arr = new int [3][3];

- int num = 10;
int[] test = null;
int[] temp = arr[1];
for(int i=0; i<temp.length; i++) {
Systehttp://m.out.print(temp[i] + " ");
};

- C0907개념02_이차원메모리구조
package C0907이차원메모리구조;
public class C0907개념02_이차원메모리구조 {
public static void main(String[] args) {
int[][] darr = new int[3][];
System.out.println(darr[0]); // null
System.out.println(darr[1]);
System.out.println(darr[2]);
int[] arr1 = {10,20,30,40,50};
int[] arr2 = {1,2,3,4};
int[] arr3 = {5,4,3,2,1};
System.out.println(arr1);
darr[0] = arr1;
darr[1] = arr2;
darr[2] = arr3;
System.out.println(darr[0]);
for(int i = 0; i < darr.length; i++) {
for(int j = 0; j < darr[i].length; j++) {
System.out.print(darr[i][j] + " ");
}
System.out.println();
}
}
}
- 메모리 그리기
- int [ ] [ ] darr = new int [3] [ ] ;

- int[] arr1 = {10,20,30,40,50};
int[] arr2 = {1,2,3,4};
int[] arr3 = {5,4,3,2,1};

- C0907개념03_이차원메모리구조
package C0907이차원메모리구조;
public class C0907개념03_이차원메모리구조 {
public static void main(String[] args) {
int[][] arr = {
{10,20,30},
{10,20,30,40,50},
{10,20,30,40}
};
// 위와같은 각 열의 크기가 다른 2차원배열을 반복문으로 만들기
int row = 3;
int[][] test = new int[row][];
//반복문을 써서. arr이 test한테 옮긴것
int[] list = {3,5,4};
for(int i = 0; i < row; i++) {
int[] temp = new int[list[i]];
int value = 10;
for(int j =0; j < list[i]; j++) {
temp[j] = value;
value += 10;
}
test[i] = temp;
}
for(int i = 0; i < test.length; i++) {
for(int j = 0; j < test[i].length; j++) {
System.out.print(test[i][j] + " ");
}
System.out.println();
}
}
}
- C0907개념04_이차원메모리구조
package C0907이차원메모리구조;
import java.util.Arrays;
/*
[문제]
아래 class1부터 class5 배열은 각 반의 시험점수를 저장한 것이다.
각 반의 총점이 높은 순서대로 세 반을 뽑아서 win배열에 저장 후,
win배열의 모든 내용을 출력하시오.
[정답]
win =
[50, 45, 25, 87, 49]
[70, 84, 64, 21, 11]
[65, 14, 24, 75, 25]
*/
public class C0907개념04_이차원메모리구조 {
public static void main(String[] args) {
int[] class1 = {10,54,65,22,15};
int[] class2 = {65,14,24,75,25};
int[] class3 = {50,45,25,87,49};
int[] class4 = {11,66,5,21,95};
int[] class5 = {70,84,64,21,11};
int[][] win = new int[3][];
//위의 배열들 중에서. 합이 가장 큰것을 3개를 넣어라. win배열에 넣어라.
int[][] data = new int[5][]; //머리만 5개
//5개를 다 연결한것
data[0] = class1;
data[1] = class2;
data[2] = class3;
data[3] = class4;
data[4] = class5;
//정렬을 해야되서. index와 total을 만든것
int[] index = new int[5];
int[] total = new int[5];
//저장 함
for(int i=0; i<5; i++) {
index[i] = i;
for(int j=0; j<5; j++) {
total[i] += data[i][j];
}
}
System.out.println("정렬 전 >>>");
System.out.println("index = " + Arrays.toString(index));
System.out.println("total = " + Arrays.toString(total));
// 정렬하기 . total를 정렬 . index도 정렬
for(int i=0; i<5; i++) {
int maxTotal = total[i];
int maxIndex = i;
for(int j=i; j<5; j++) {
if(maxTotal < total[j]) {
maxTotal = total[j];
maxIndex = j;
}
}
int temp = total[i];
total[i] = total[maxIndex];
total[maxIndex] = temp;
temp = index[i];
index[i] = index[maxIndex];
index[maxIndex] = temp;
}
System.out.println("정렬 후 >>>");
System.out.println("index = " + Arrays.toString(index));
System.out.println("total = " + Arrays.toString(total));
for(int i=0; i<3; i++) {
win[i] = data[index[i]];
}
for(int i=0; i<3; i++) {
System.out.println(Arrays.toString(win[i]));
}
}
}
- C0907개념05_이차원메모리구조
package C0907이차원메모리구조;
import java.util.Arrays;
/*
[문제]
아래 class1부터 class5 배열은 각 반의 시험점수를 저장한 것이다.
각 반의 총점이 높은 순서대로 세 반을 뽑아서 win배열에 저장 후,
win배열의 모든 내용을 출력하시오.
[정답]
win =
[50, 45, 25, 87, 49]
[70, 84, 64, 21, 11]
[65, 14, 24, 75, 25]
*/
public class C0907개념05_이차원메모리구조 {
public static void main(String[] args) {
int[][] classList = {
{10,54,65,22,15},
{65,14,24,75,25},
{50,45,25,87,49},
{11,66,5,21,95},
{70,84,64,21,11}
};
int[][] win = new int[3][];
int[] index = new int[5];
int[] total = new int[5];
for(int i=0; i<classList.length; i++) {
index[i] = i;
for(int j=0; j<classList[i].length; j++) {
total[i] += classList[i][j];
}
}
for(int i=0; i<classList.length; i++) {
int maxTotal = total[i];
int maxIndex = i;
for(int j=i; j<classList.length; j++) {
if(maxTotal < total[j]) {
maxTotal = total[j];
maxIndex = j;
}
}
int temp = total[i];
total[i] = total[maxIndex];
total[maxIndex] = temp;
temp = index[i];
index[i] = index[maxIndex];
index[maxIndex] = temp;
}
System.out.println("index = " + Arrays.toString(index));
System.out.println("total = " + Arrays.toString(total));
for(int i=0; i<3; i++) {
win[i] = classList[index[i]];
}
System.out.println("정답 >>>");
for(int i=0; i<3; i++) {
System.out.println(Arrays.toString(win[i]));
}
}
}
- C0907개념06_이차원메모리구조
package C0907이차원메모리구조;
public class C0907개념06_이차원메모리구조 {
public static void main(String[] args) {
int[] arr = {10,20,30};
int[][] darr = {
{10,20,30},
{20,30,40},
{30,40,50}
};
darr[0] = arr;
darr[1] = arr;
darr[2] = arr;
arr[1] = 100;
// [문제] 아래 주석을 풀고 실행했을 때 어떤 값이 나올지 예상하고 주석을 푸시오.
/*
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
System.out.print(darr[i][j] + " ");
}
System.out.println();
}
*/
}
}
- C0907개념07_이차원메모리구조
package C0907이차원메모리구조;
import java.util.Arrays;
import java.util.Random;
public class C0907개념07_이차원메모리구조 {
public static void main(String[] args) {
int[] arr1 = {10,20,30};
int[] arr2 = {40,50,60};
int[] arr3 = {70,80,90};
int[][] darr = new int[3][];
darr[0] = arr1;
darr[1] = arr2;
darr[2] = arr3;
Random ran = new Random();
for(int i = 0; i < 1000; i++) {
int r1 = ran.nextInt(3); // 0
int r2 = ran.nextInt(3); // 1
int r3 = ran.nextInt(3); // 2
int r4 = ran.nextInt(3); // 1
int temp = darr[r1][r2];
darr[r1][r2] = darr[r3][r4];
darr[r3][r4] = temp;
}
// [문제] 아래 주석을 풀고 실행했을 때 어떤 값이 나올지 예상하고 주석을 푸시오.
/*
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr3));
*/
}
}
- C0907개념08_이차원메모리구조
package C0907이차원메모리구조;
public class C0907개념08_이차원메모리구조 {
public static void main(String[] args) {
//int[][] darr = new int[3][3]; // 13개
int[][] darr = new int[3][]; // 4개
int[] arr = {10,20,30};
darr[0] = arr;
darr[1] = arr;
darr[2] = arr;
//10,20,30
//10,20,30
//10,20,30
//----------------------
arr[1] = 100;
if(darr[0] == darr[2]) {
System.out.println("o");
}else {
System.out.println("x");
}
//---------------------------------
int darr2[][] = {
{10,20,30},
{40,50,60},
{70,80,90}
};
// 13개
int temp[][] = new int[3][];
// 17개
temp[0] = darr2[1];
temp[1] = darr2[2];
temp[2] = darr2[0];
for(int i = 0; i < 3; i++) {
System.out.print(temp[0][i] + " ");
}
System.out.println();
darr2[1] = null;
for(int i = 0; i < 3; i++) {
System.out.print(temp[1][i] + " ");
}
}
}
- C0908행렬
- 중요하다.
- C0908개념01_압축하기
package C0908행렬;
import java.util.Arrays;
/*
[문제]
아래 before배열은 압축하기 전의 데이터이다.
연속으로 이뤄진 데이터를 모아 압축하려 한다.
예)
33 => {3, 2}
55555 => {5, 5}
...
after 배열의 0번은 압축할 값, 1번은 연속된 수의 개수를 저장하고,
그 값을 전부 출력하시오.
[정답]
33 ==> {3,2}
55555 ==> {5,5}
3 ==> {3,1}
444 ==> {4,3}
*/
public class C0908개념01_압축하기 {
public static void main(String[] args) {
int[] before = {3, 3, 5, 5, 5, 5, 5, 3, 4, 4, 4};
int[][] after = new int[4][2];
// after[0][0] 0열 : 숫자(3)
// after[0][1] 1열 : 개수(2)
//시작값을 넣고 시작한것
after[0][0] = before[0];
after[0][1] = 1;
/*
after = [
[3, 1],
[0, 0],
[0, 0],
[0, 0],
]
y = 0, i = 1 after[0][0] == before[1] true
after = [
[3, 2],
[0, 0],
[0, 0],
[0, 0],
]
y = 0, i = 2 after[0][0] == before[2] false
y = 1 after[1][0] = 5
after[1][1] = 1
after = [
[3, 2],
[5, 1],
[0, 0],
[0, 0],
]
....
*/
int y = 0;
for(int i=1; i<before.length; i++) {
if(after[y][0] == before[i]) {
after[y][1] += 1;
} else {
y += 1;
after[y][0] = before[i];
after[y][1] = 1;
}
}
for(int i=0; i<after.length; i++) {
System.out.println(Arrays.toString(after[i]));
}
}
}
- C0908개념02_압축풀기
package C0908행렬;
import java.util.Arrays;
/*
[문제]
아래 arr배열은 압축한 데이터이다.
데이터는 2개씩 짝을 이룬다는 규칙이 있다.
(1) 앞의 숫자 : 데이터
(2) 뒤의 숫자 : 개수
예)
{3, 2} => 33
{5, 6} => 555555
...
결국 실제 데이터는 335555552444 인 것이다.
아래 temp 배열을 위 데이터의 크기만큼 생성하고,
각각의 값을 저장 후 출력하시오.
[정답]
temp = {3,3,5,5,5,5,5,5,2,4,4,4}
*/
public class C0908개념02_압축풀기 {
public static void main(String[] args) {
int[][] arr = {
{3, 2},
{5, 6},
{2, 1},
{4, 3}
};
int[] temp = null;
// 1. temp배열의 길이 구하기
int count = 0;
for(int i=0; i<arr.length; i++) {
count += arr[i][1];
}
System.out.println("count = " + count);
// 2. 1번에서 구한 길이만큼의 temp배열을 생성
temp = new int[count];
// 3. 문제에서 요구한 데이터를 temp배열에 저장
/*
index = 0
i = 0, value=3, size=2
j = 0 temp = [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] index = 1
j = 2 temp = [3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] index = 2
i = 1, value=5, size=6
j = 0 temp = [3, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0] index = 3
j = 1 temp = [3, 3, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0] index = 4
....
*/
int index = 0;
for(int i=0; i<arr.length; i++) {
int value = arr[i][0]; // 3값
int size = arr[i][1]; // 2회
for(int j=0; j<size; j++) {
temp[index] = value;
index += 1;
}
}
System.out.println(Arrays.toString(temp));
}
}
- C0908행렬_문제
- C0908문제03
package C0908행렬_문제;
/*
[문제]
scoreList 배열은 학생 6명의 점수이다.
이 데이터를 그래프로 표현하려 한다.
표시는 10자리 숫자로 표현해서 개수만큼 * 로 출력한다.
전에는 왼쪽에서 오른쪽으로 가로로 표현했지만,
이번에는 아래에서 위로 출력해보자.
[정답]
*
*
*
* *
* *
* * *
* * *
* * * *
* * * * *
* * * * *
31 76 54 2 100 23
*/
public class C0908문제03 {
public static void main(String[] args) {
int[] scoreList = {31, 76, 54, 2, 100, 23};
int[][] temp = new int[10][scoreList.length];
//10때문에 세로로 10줄이다.
//점수를 10으로 나눈 값만큼 *을 세로 방향으로 아래에서 위로 쌓기 위해, temp 배열에 1을 채우는 작업
//그래프 만드는데. *를 몇 개가 나와야 하는지를 temp배열에 담기
//*을 몇 개 찍어야 하는지를 계산해서, temp 배열에 그 개수만큼 1을 아래에서 위로 채워 넣는 과정입니다.
for(int i=0; i<temp.length; i++) {
for(int j=0; j<temp[i].length; j++) {
if(scoreList[j] / 10 > i) {
temp[temp.length - 1 - i][j] = 1;
//배열 출력은 위에서 아래로 되므로,
//시각적으로 아래에서 위로 보이게 하려면 배열의 인덱스를 뒤집어야 합니다.
}
}
}
//지금까지 채운 temp 배열을 콘솔에 출력해서 확인하는 디버깅용 코드
//2차원 배열 temp의 현재 상태를 출력해서, 각 위치에 1이 들어갔는지를 확인하려는 것
//즉, 별(*) 출력 전에 temp 배열을 숫자로 미리 확인해보는 용도입니다.
for(int i=0; i<temp.length; i++) { //10줄
for(int j=0; j<temp[i].length; j++) {
System.out.print(temp[i][j] + " ");
}
System.out.println();
}
System.out.println();
//별(*)로 막대 그래프를 출력하는 핵심 출력부
//temp 배열에 저장한 1값들을 기준으로 시각적인 세로 그래프를 출력
for(int i=0; i<temp.length; i++) {
for(int j=0; j<temp[i].length; j++) {
if(temp[i][j] == 1) {
System.out.print(" * ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for(int i=0; i<scoreList.length; i++) {
System.out.print(scoreList[i] + " ");
}
}
}
반응형
'코딩 > 2-JAVA' 카테고리의 다른 글
| C0910이차배열입력 (5) | 2025.06.27 |
|---|---|
| C0909이차배열과데이터 (3) | 2025.06.27 |
| C0906일차원메모리구조 (2) | 2025.06.26 |
| C0903정렬 , C0904이차배열완전탐색 , C0905그룹바이 (3) | 2025.06.25 |
| C09이차배열 => C0901이차배열 , C0902이차배열과데이터 (0) | 2025.06.25 |