-
package day057; import java.util.ArrayList; /* * # 배열의 한계 * - 배열은 한번 선언하면 프로그램에서 그 크기를 바꿀 수 없다. * * # java.util.ArrayList * - ArrayList 클래스는 데이터가 입력되면, 자동으로 크기가 커지고 * - 데이터가 제거되면, 자동으로 크기가 작아진다. * - 중간에 데이터가 삽입되면, 데이터가 삽입될 위치부터 모든 데이터가 뒤로 이동되고 * - 중간의 데이터가 제거된 다음 위치부터 모든 데이터가 앞으로 이동한다. * * # <E> * - 제네릭(generic)이라 부르며 ArrayList에 저장될 데이터 타입을 * 반드시 클래스 타입으로 작성한다. * - 기본 자료형 데이터를 저장하는 ArrayList를 만들어야 하는 경우에는 * 래퍼 클래스를 사용한다. * * ArrayList list = new ArrayList(); // JDK 1.4 이전 * ArrayList<Integer> list = new ArrayList<Integer>(); // JDK 1.5 이후 * ArrayList<Integer> list = new ArrayLIst<>(); // JDK 1.7 이후 * * // ============ 주요 기능 =============== * // 1) add * // 2) remove * // 3) clear * // 4) size * // 5) get * // 6) set */ public class Day05701리스트 { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); // add(value) : ArrayList의 맨 뒤에 value를 추가한다. list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); System.out.println(list); // remove(index) : ArrayList의 index번째 데이터를 제거한다. list.remove(3); System.out.println(list); // size() : ArrayList에 저장된 데이터의 개수를 얻어온다. int count = list.size(); System.out.println("count = " + count); // get() : ArrayList의 index번째의 value를 얻어온다. System.out.print("["); for(int i=0; i<count; i++) { System.out.print(list.get(i)); if(i != count - 1) { System.out.print(", "); } } System.out.println("]"); // add(index, value) : ArrayList의 index번째 위치에 value를 삽입한다. list.add(0, 9); System.out.println(list); // set(index, value) : ArrayList의 index번째 위치에 value를 수정한다. list.set(3, 5); System.out.println(list); // clear() : ArrayList의 모든 데이터를 제거한다. list.clear(); System.out.println(list.size()); } } package day057; import java.util.ArrayList; import java.util.Scanner; public class Day05702리스트실습1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<Integer>(); while(true) { System.out.println(list); System.out.println("1.추가 2.삭제 3.삽입 4.종료"); int sel = scan.nextInt(); if(sel == 1) { System.out.println("[추가]숫자 입력"); int num = scan.nextInt(); list.add(num); } else if(sel == 2) { System.out.println("[삭제]인덱스 입력"); int index = scan.nextInt(); if(list.size() <= 0) { continue; } if(list.size() <= index || index < 0) { continue; } list.remove(index); } else if(sel == 3) { System.out.println("[삽입]위치 입력"); int pos = scan.nextInt(); System.out.println("[삽입]숫자 입력"); int value = scan.nextInt(); list.add(pos, value); } else if(sel == 4) { System.out.println("종료"); break; } } } } package day057; import java.util.Scanner; class MyArray { int[] arr; int count; void print() { System.out.print("["); for(int i=0; i<count; i++) { System.out.print(arr[i]); if(i != count - 1) { System.out.print(","); } } System.out.println("]"); } void add(int value) { if(count == 0) { arr = new int[count + 1]; }else if(count > 0) { int[] temp = arr; arr = new int[count + 1]; for(int i=0; i<count; i++) { arr[i] = temp[i]; } temp = null; } arr[count] = value; count = count + 1; } void add(int index, int value) { if(count == 0) { arr = new int[count + 1]; }else if(count > 0) { int[] temp = arr; arr = new int[count + 1]; int j = 0; for(int i=0; i<count + 1; i++) { if(i != index) { arr[i] = temp[j]; j = j + 1; } } temp = null; } arr[index] = value; count = count + 1; } void remove(int index) { if(count == 1) { arr = null; }else if(count > 1) { int[] temp = arr; arr = new int[count - 1]; int j = 0; for(int i=0; i<count; i++) { if(i != index) { arr[j] = temp[i]; j = j + 1; } } temp = null; } count = count - 1; } int size() { return count; } } public class Day05703리스트직접만들기1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); MyArray list = new MyArray(); while(true) { list.print(); System.out.println("1.추가 2.삭제 3.삽입 4.종료"); int sel = scan.nextInt(); if(sel == 1) { System.out.println("[추가]숫자 입력"); int value = scan.nextInt(); list.add(value); } else if(sel == 2) { System.out.println("[삭제]위치 입력"); int index = scan.nextInt(); if(list.size() <= 0) { return; } if(list.size() <= index || index < 0) { return; } list.remove(index); } else if(sel == 3) { System.out.println("[삽입]위치 입력"); int index = scan.nextInt(); System.out.println("[삽입]숫자 입력"); int value = scan.nextInt(); list.add(index, value); } else if(sel == 4) { System.out.println("종료"); break; } } } } package day057; import java.util.Vector; public class Day05704벡터 { public static void main(String[] args) { // Vector 는 ArrayList와 똑같은것이다. // (ArrayList 이전에 만들어진것으로 ArrayList 가 성능이 더좋다.) Vector<Integer> list = new Vector<>(); // add(value) : Vector의 맨 뒤에 value를 추가한다. list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); System.out.println(list); // remove(index) : Vector의 index번째 데이터를 제거한다. list.remove(3); System.out.println(list); // size() : Vector에 저장된 데이터의 개수를 얻어온다. int count = list.size(); System.out.println("count = " + count); // get() : Vector의 index번째의 value를 얻어온다. System.out.print("["); for(int i=0; i<count; i++) { System.out.print(list.get(i)); if(i != count - 1) { System.out.print(", "); } } System.out.println("]"); // add(index, value) : Vector의 index번째 위치에 value를 삽입한다. list.add(0, 9); System.out.println(list); // set(index, value) : Vector의 index번째 위치에 value를 수정한다. list.set(3, 5); System.out.println(list); // clear() : Vector의 모든 데이터를 제거한다. list.clear(); System.out.println(list.size()); } } package day057; import java.util.ArrayList; class Tv { String name; String brand; int price; Tv(String name, String brand, int price) { this.name = name; this.brand = brand; this.price = price; } } public class Day05705리스트실습2 { public static void main(String[] args) { ArrayList<Tv> list = new ArrayList<>(); Tv temp = new Tv("TV", "삼성", 1000); list.add(temp); temp = new Tv("시그니처TV", "엘지", 2000); list.add(temp); temp = new Tv("스마트TV", "애플", 3000); list.add(temp); temp = list.get(1); System.out.println(temp.name); } } package day057; class MyArrayList { Tv[] arr; int count; void add(Tv e) { if (count == 0) { arr = new Tv[count + 1]; } else if (count > 0) { Tv[] temp = arr; arr = new Tv[count + 1]; for (int i = 0; i < count; i++) { arr[i] = temp[i]; } } arr[count] = e; count += 1; } int size() { return count; } void remove(int index) { if (count == 1) { arr = null; } else if (count > 1) { Tv[] temp = arr; arr = new Tv[count - 1]; int j = 0; for (int i = 0; i < count; i++) { if (i != index) { arr[j] = temp[i]; j += 1; } } } count -= 1; } Tv get(int index) { return arr[index]; } } public class Day05706리스트직접만들기2 { public static void main(String[] args) { MyArrayList myList = new MyArrayList(); Tv temp = new Tv("TV", "삼성", 1000); myList.add(temp); temp = new Tv("시그니처TV", "엘지", 2000); myList.add(temp); temp = new Tv("스마트TV", "애플", 3000); myList.add(temp); temp = myList.get(1); System.out.println(temp.name); } } package day057; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.Scanner; class Student { String id; String pw; void setData(String id, String pw) { this.id = id; this.pw = pw; } void printData() { System.out.println("아이디 : " + id + " 비밀번호 : " + pw); } } class Manager { ArrayList<Student> list = new ArrayList<>(); void addStudent(Student student) { list.add(student); } Student removeStudent(int index) { Student delStudent = list.get(index); list.remove(index); return delStudent; } int checkId(Student student) { int check = -1; for(int i=0; i<list.size(); i++) { if(list.get(i).id.equals(student.id)) { check = i; break; } } return check; } void printStudent() { for(int i=0; i<list.size(); i++) { list.get(i).printData(); } } String outData() { String data = ""; int count = list.size(); if(count == 0) { return data; } data = data + count; data = data + "\n"; for(int i=0; i<count; i++) { data = data + list.get(i).id; data = data + ","; data = data + list.get(i).pw; if(count - 1 != i) { data = data + "\n"; } } return data; } void loadData(ArrayList<Student> list) { this.list = list; } int getSize() { return list.size(); } void sortData() { /* 직접 구현해보세요. */ } } public class Day05707리스트실습3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Manager manager = new Manager(); while(true) { System.out.println("1.가입 2.탈퇴 3.정렬 4.출력 5.저장 6.로드"); int sel = scan.nextInt(); if(sel == 1) { Student temp = new Student(); System.out.println("[가입] id 를 입력하세요 >>> "); temp.id = scan.next(); int check = manager.checkId(temp); if(check == -1) { System.out.println("[가입] pw 를 입력하세요 >>> "); temp.pw = scan.next(); manager.addStudent(temp); System.out.println(temp.id + "님 가입을 환영합니다."); }else { System.out.println("중복아이디 입니다."); } }else if(sel == 2) { manager.printStudent(); Student temp = new Student(); System.out.println("[탈퇴] id 를 입력하세요 >>> "); temp.id = scan.next(); int check = manager.checkId(temp); if(check == -1) { System.out.println("없는 아이디입니다."); }else { Student del_st = manager.removeStudent(check); System.out.println(del_st.id + "님 탈퇴 되었습니다."); } }else if(sel == 3) { manager.sortData(); manager.printStudent(); }else if(sel == 4) { manager.printStudent(); }else if(sel == 5) { if(manager.getSize() == 0)return; try { FileWriter fw =new FileWriter("student_manager_vec.txt"); String data = manager.outData(); if(!data.equals("")) { fw.write(data); System.out.println(data); } fw.close(); } catch (Exception e) {e.printStackTrace();} }else if(sel == 6) { try { FileReader fr = new FileReader("student_manager_vec.txt"); BufferedReader br = new BufferedReader(fr); String line = br.readLine(); int count = Integer.parseInt(line); ArrayList<Student> list = new ArrayList<Student>(); for(int i = 0; i < count; i++) { Student temp = new Student(); line = br.readLine(); String value[] = line.split(","); temp.id = value[0]; temp.pw = value[1]; list.add(temp); } fr.close(); br.close(); manager.loadData(list); manager.printStudent(); } catch (Exception e) {e.printStackTrace();} } } } } package day057; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; class Node { int front; int back; } class MemoryGame { Scanner scan = new Scanner(System.in); final int SIZE = 10; ArrayList<Node> nodeList; int[] data; int gameCount; void dataInit() { data = new int[SIZE]; for(int i=0; i<SIZE; i++) { data[i] = (i + 2) / 2; } } void nodeListInit() { nodeList = new ArrayList<Node>(); for(int i=0; i<SIZE; i++) { Node node = new Node(); node.front = data[i]; nodeList.add(node); } } void shuffle() { Random ran = new Random(); for(int i=0; i<1000; i++) { int r = ran.nextInt(SIZE); int temp = data[0]; data[0] = data[r]; data[r] = temp; } } void init() { dataInit(); shuffle(); nodeListInit(); } void print() { printBlank(); for(int i=0; i<SIZE; i++) { if(nodeList.get(i).back == 0) { System.out.print("[ ]"); }else { System.out.print("[" + nodeList.get(i).back + "]"); } } System.out.println(); } void printBlank() { for(int i=0; i<100; i++) { System.out.println(); } } void printAnswer() { System.out.println("[기억력 게임 : 정답이 5초 뒤에 사라집니다]"); for(int i=0; i<SIZE; i++) { System.out.print("[" + nodeList.get(i).front + "]"); } System.out.println(); try { Thread.sleep(5000); }catch(Exception e) { e.printStackTrace(); } } void update() { printAnswer(); while(true) { print(); if(gameCount == SIZE/2) { System.out.println("게임을 종료합니다."); break; } System.out.println("인덱스1 입력"); int sel1 = scan.nextInt(); System.out.println("인덱스2 입력"); int sel2 = scan.nextInt(); nodeList.get(sel1).back = nodeList.get(sel1).front; nodeList.get(sel2).back = nodeList.get(sel2).front; print(); try { Thread.sleep(1000); }catch(Exception e) { e.printStackTrace(); } if(nodeList.get(sel1).back != nodeList.get(sel2).back) { nodeList.get(sel1).back = 0; nodeList.get(sel2).back = 0; }else { gameCount = gameCount + 1; } } } } public class Ex14 { public static void main(String[] args) { MemoryGame mg = new MemoryGame(); mg.init(); mg.update(); } } package day057; import java.util.ArrayList; class Node1to50 { int num; void printNode() { System.out.print(num + "\t"); } } class Manager1to50 { ArrayList<Node1to50[]> nodeList; int[][] data; final int SIZE = 5; void dataInit() { data = new int[SIZE][SIZE]; int num = 1; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { data[i][j] = num; // System.out.println(num); num += 1; } } } void dataShuffle() { } void nodeInit() { nodeList = new ArrayList<>(); for (int i = 0; i < SIZE; i++) { Node1to50[] temp = new Node1to50[SIZE]; for (int j = 0; j < SIZE; j++) { Node1to50 node = new Node1to50(); node.num = data[i][j]; // System.out.println(node.num); temp[j] = node; } nodeList.add(temp); } } void printNodeList() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { nodeList.get(i)[j].printNode(); } System.out.println(); } } void init() { dataInit(); dataShuffle(); nodeInit(); printNodeList(); } } public class Day057091to50 { public static void main(String[] args) { // 1to50 을만들어보세요 Manager1to50 nm = new Manager1to50(); nm.init(); } }