반응형
- C14콜렉션
- C1401가변배열_개념
- C1401개념01가변배열컨트롤러
package C1401가변배열_개념;
import java.util.Scanner;
/*
# 배열 컨트롤럴(무한배열) : 벡터(Vector)
1. 추가
값을 입력받아 순차적으로 추가
2. 삭제(인덱스)
인덱스를 입력받아 해당 위치의 값 삭제
3. 삭제(값)
값을 입력받아 삭제
없는 값 입력 시 에외처리
4. 삽입
인덱스와 값을 입력받아 삽입
*/
//가변 배열
//원래는 배열의 크기를 정해두고 하는데.
//가변배열은 크기를 1로 저장하고 그때그때마다 크기를 늘리는것
//temp에 주소를 저장하고 더 만드는것
public class C1401개념01가변배열컨트롤러 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] score = null;
int count = 0;
while(true) {
for(int i=0; i<count; i++) {
System.out.print(score[i] + " ");
}
System.out.println();
System.out.println("[벡터 컨트롤러]");
System.out.println("[1]추가");
System.out.println("[2]삭제(인덱스)");
System.out.println("[3]삭제(값)");
System.out.println("[4]삽입");
System.out.println("[0]종료");
System.out.print("메뉴 선택 : ");
int sel = scan.nextInt();
if(sel == 0) {
break;
} else if(sel == 1) {
//가변배열 공식
if(count == 0) {
//처음에는 1개를 만든다.
score = new int[count + 1];
} else {
//2개부터는 여기서 진행
//일단. 전에꺼 temp에게 전달 . 주소
int[] temp = score;
score = new int[count + 1];
//나는 한개 더 크게 만든다. 하지만 비여있다.
for(int i=0; i<count; i++) {
//temp에게 지난 값을 전달 받는다.
score[i] = temp[i];
}
temp = null;
//temp의 지난 값을 삭제
}
System.out.print("[추가]성적 입력 : ");
int data = scan.nextInt();
score[count] = data; //추가된 값 넣기
count += 1;
//--------------------------------
} else if(sel == 2) {
System.out.print("[삭제]인덱스 입력 : ");
int index = scan.nextInt();
if(count <= index || index < 0) {
//아예 없으면 삭제를 못한다.
//이상한 숫자면 삭제 못함
System.out.println("[해당 위치는 삭제할 수 없습니다.]");
continue;
}
//삭제
if(count == 1) {
//1개일 때는 지운다. /한개면 지우면 끝
score = null;
} else if(count > 1) {
//1개가 아니라. 여러개.
//temp가 가지고 있어라.
//temp에 지난 배열을 전달
int[] temp = score;
score = new int[count - 1];
//나는 한 치수 작게 만든다. 데이터를 만든다.
int j = 0;
//지우는거 빼고 나머지 복사
for(int i=0; i<count; i++) {
if(i != index) {
score[j] = temp[i];
j += 1;
}
}
temp = null; //지난 데이터는 삭제
}
count -= 1;
} else if(sel == 3) {
System.out.print("[삭제]값 입력 : ");
int data = scan.nextInt();
int index = -1;
for(int i=0; i<count; i++) {
if(score[i] == data) {
index = i;
break;
}
}
if(index == -1) {
System.out.println("[입력하신 값은 존재하지 않습니다.]");
continue;
}
if(count == 1) {
score = null;
} else {
int[] temp = score;
score = new int[count - 1];
int j = 0;
for(int i=0; i<count; i++) {
if(i != index) {
score[j] = temp[i];
j += 1;
}
}
temp = null;
}
count -= 1;
} else if(sel == 4) {
System.out.print("[삽입]인덱스 입력 : ");
int index = scan.nextInt();
if(count < index || index < 0) {
System.out.println("[해당 위치에는 삽입할 수 없습니다.]");
continue;
}
System.out.println("[삽입]값 입력 : ");
int data = scan.nextInt();
if(count == 0) {
score = new int[count + 1];
} else if(count > 0) {
int[] temp = score;
score = new int[count + 1];
int j = 0;
for(int i=0; i<count + 1; i++) {
if(i != index) {
score[i] = temp[j];
j += 1;
}
}
temp = null;
}
score[index] = data;
count +=1;
}
}
scan.close();
}
}
- 가변배열
- 자바 배열 1번 방법
- 개수 1000개 만들어서
- index = 0
- 사용한 만큼 보여주는거
- 2번
- 갯수 만큼 만드는것
- int [ ] = arr = new int [1];
- int [ ] = temp; //맡김
- arr = new int [ 2 ]
- for // 탬프 돌려주기
- //가변 배열
- 자바 배열 1번 방법


- C1401개념02학생관리
package C1401가변배열_개념;
import java.util.Scanner;
public class C1401개념02학생관리 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] numberList = null; // 번호
int[][] scoreList = null; // 성적 3개
int count = 0;
int num = 1001;
while(true) {
for(int i=0; i<count; i++) {
System.out.print(numberList[i] + " ");
for(int j= 0; j < 3 ; j++) {
System.out.print(scoreList[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("[벡터 컨트롤러]");
System.out.println("[0]종료");
System.out.println("[1]추가");
System.out.println("[2]삭제(인덱스)");
System.out.println("[3]삭제(학생번호)");
System.out.println("[4]삽입");
System.out.print("메뉴 선택 : ");
int sel = scan.nextInt();
if(sel == 0) {
break;
}
else if(sel == 1) {
if(count == 0) {
numberList = new int[1];
scoreList= new int[1][3];
}else {
int[] numTemp = numberList;
int[][] scoreTemp = scoreList;
numberList = new int[count + 1];
scoreList = new int[count + 1][];
scoreList[count] = new int[3];
for(int i = 0; i < count; i++) {
numberList[i] = numTemp[i];
scoreList[i] = scoreTemp[i];
}
}
numberList[count] = num;
for(int i = 0; i < 3; i++) {
System.out.println("[추가] 과목" + (i + 1) + " 점수 : ");
scoreList[count][i] = scan.nextInt();
}
num += 1;
count += 1;
}
else if(sel == 2) {
if(count > 0) {
System.out.println("[삭제] 인덱스 입력 : ");
int check = scan.nextInt();
if(0 <= check && check < count) {
int[] numTemp = numberList;
int[][] scoreTemp = scoreList;
numberList = new int[count - 1];
scoreList = new int[count - 1][3];
int index = 0;
for(int i=0; i<numTemp.length; i++) {
if(i != check) {
numberList[index] = numTemp[i];
index += 1;
}
}
index = 0;
for(int i=0; i<scoreTemp.length; i++) {
if(i != check) {
scoreList[index] = scoreTemp[i];
index += 1;
}
}
count -= 1;
} else {
System.out.println("인덱스를 확인해주세요.");
}
} else {
System.out.println("삭제할 정보가 없습니다.");
}
}
else if(sel == 3) {
if(count > 0) {
System.out.println("[삭제] 번호 입력 : ");
int number = scan.nextInt();
int check = -1;
for(int i=0; i<count; i++) {
if(numberList[i] == number) {
check = i;
break;
}
}
if(check == -1) {
System.out.println("번호를 확인해주세요.");
} else {
int[] numTemp = numberList;
int[][] scoreTemp = scoreList;
numberList = new int[count - 1];
scoreList = new int[count - 1][3];
int index = 0;
for(int i=0; i<numTemp.length; i++) {
if(i != check) {
numberList[index] = numTemp[i];
index += 1;
}
}
index = 0;
for(int i=0; i<scoreTemp.length; i++) {
if(i != check) {
scoreList[index] = scoreTemp[i];
index += 1;
}
}
count -= 1;
}
} else {
System.out.println("삭제할 정보가 없습니다.");
}
}
else if(sel == 4) {
System.out.print("[삽입] 인덱스 입력 : ");
int index = scan.nextInt();
if(0 <= index && index <= count) {
if(count == 0) {
numberList = new int[1];
scoreList= new int[1][3];
}else {
int[] numTemp = numberList;
int[][] scoreTemp = scoreList;
numberList = new int[count + 1];
scoreList = new int[count + 1][];
scoreList[index] = new int[3];
int j = 0;
for(int i = 0; i < count + 1; i++) {
if(i != index) {
numberList[i] = numTemp[j];
scoreList[i] = scoreTemp[j];
j += 1;
}
}
}
numberList[index] = num;
for(int i = 0; i < 3; i++) {
System.out.println("[추가] 과목" + (i + 1) + " 점수 : ");
scoreList[index][i] = scan.nextInt();
}
num += 1;
count += 1;
} else {
System.out.println("해당 위치에는 삽입할 수 없습니다.");
}
}
}
scan.close();
}
}
반응형
'코딩 > 2-JAVA' 카테고리의 다른 글
| C16캡슐화 (5) | 2025.07.10 |
|---|---|
| C1404콜렉션벡터_개념 (0) | 2025.07.09 |
| C13파일입출력 (1) | 2025.07.07 |
| C1203메서드클래스배열_개념 ~ C1208메서드클래스배열_암기5 (0) | 2025.07.04 |
| C1202메서드리턴_개념 (1) | 2025.07.03 |