05.19 TIL

JuHyung Yoon·2023년 5월 19일
0

JAVA 기초

목록 보기
10/11

Set은 데이터 저장에 순서가 없다. 동일한 값을 중복하여 저장할 수 없다.

Set<String> set = new HashSet<>();
		set.add("트와이스");
		set.add("뉴진스");
		set.add("에스파");
		set.add("소녀시대");
		set.add("원더걸스");
		set.add("뉴진스");
		System.out.println("set 의 크기 = " + set.size());
		System.out.println("set 에 저장된 문자열 = " + set);

출력 결과 : 5
set에 저장된 문자열은 해쉬코드값이 달라 실행 컴퓨터마다 다르게 무작위로 나온다

Iterator<String> setIterator = set.iterator();	

은 제네릭타입 Iterator의 타입을 결정해줌
iterator는 데이터를 가져오는 집게

대표적인 map 의 예시: 객체의 식별값 해시코드(key)로 객체의 메모리 주소(value)를 찾아 데이터접근 객체가 생성되면 메모리 할당하고 그 주소로 해시함수를 실행하면 해시코드가 만들어집니다.해시테이블이라는 메모리 영역에 key 와 value값을 저장합니다
Map 은 key-value 한쌍으로 데이터를 저장하면, key를 이용하여 value를 가져올 수 있다.
key는 동일한 값을 중복하여 저장할 수 없다.

지정파일 읽기 입출력

####파일 읽기

String path = 파일 주소
File file = new File(path) 경로를 받아주는 객체 생성

Scanner fsc = null; ??비어있을수가 없어서?


try{
fsc = new Scnnr(file)

while(fsc.hasNext) //문자를 가지고 있으면 true로 계속 반복
System.out.println(fsc.nextLine()); 한줄씩 출력 메소드
} catch(FileNotFoundException e) {	//예외가 발생한 정보를 Exceptopn 객체에 저장
System.out.println("파일로부터 읽어오기 - 입력 예외 : " + e.getMessage()); //FilenotFoundExceptopn e 에 저장한 정보를 getMessage로 불러옴
}finally{
fsc.close();
}

파일 쓰기(실행할때 마다 덮어쓰기)

String path = 파일 주소
File file = new file(path)
Scanner sc = new Scanner(System.in);		//표준입력 스캐너 객체

PrintWriter fpw = null;
try {
			fpw = new PrintWriter(file);
while(true) {
				System.out.printf("자바에서 쓰는 영어 단어 입력 >>>>");
				String english = sc.nextLine();
				if(english.equals("_end")) {
					break;
				}
				System.out.println("단어의 의미를 입력하세요.>>>>");
				String Korean = sc.nextLine();
				
				fpw.print(english);
				fpw.print("/");
				fpw.println(Korean);
				
				
				System.out.println("1개 단어 추가 완료");
			}
			
			System.out.println("파일 등록 완료!");
		}catch(FileNotFoundException e) {
			System.out.println("파일로부터 읽어오기 - 입력 예외 : " + e.getMessage());
		}finally {
			if(fpw != null) {
			}
			fpw.close();
		}
profile
ㅋㅎ딩초보

0개의 댓글