오늘 아파서 수업 중간에 들어옴
Ex11Map1.java
import java.util.*;
class Ex11Map1
{
public static void main(String[] args)
{
HashMap<String,String> map=new HashMap<String,String>();
map.put("castello","1234");
map.put("kimchi","1234");
map.put("kimchi","5678");
//String pw = map.get("kimchi");
//System.out.println(pw);
Scanner scan = new Scanner(System.in);
while(true)
{
System.out.println("Enter your id: ");
String id=scan.nextLine().trim();
System.out.print("Enter your pw: ");
String pw=scan.nextLine().trim();
System.out.println();
if(!map.containsKey(id))
{
System.out.println("Wrong id! Reenter id:");
continue;
}
else
{
if(!(map.get(id)).equals(pw))
{
System.out.println("Wrong pw!!Reenter pw!!");
}
else
{
System.out.println("Login Successful!");
break;
}
}
}
scan.close();
}
}
Ex11Map2
import java.util.*;
class Ex11Map2
{
public static void main(String[] args)
{
HashMap<String,Integer>map=new HashMap<String,Integer>();
map.put("김자바",90);
map.put("이자바",100);
map.put("강자바",80);
map.put("안자바",70);
Set set=map.entrySet();
Iterator it = set.iterator();
while(it.hasNext()) {
Map.Entry e=(Map.Entry)it.next();
//타입 캐스팅을 해주는 이유? Map안에는 Entry가 있고 그 안에 키와 값이 있다.
but next()를 하면 다음 값이 뭔지 알수 없음
System.out.println("Name:"+e.getKey()+"Score: "+e.getValue());
}
}
}
import java.util.*;
class Ex11Map2
{
public static void main(String[] args)
{
HashMap<String,Integer>map=new HashMap<String,Integer>();
map.put("김자바",90);
map.put("이자바",100);
map.put("강자바",80);
map.put("안자바",70);
Set set=map.entrySet();
/*
Iterator it = set.iterator();
while(it.hasNext()) {
Map.Entry e=(Map.Entry)it.next();
System.out.println("Name:"+e.getKey()+"Score: "+e.getValue());
}
*/
/* way2 keyset()를 사용하긔 */
Iterator<String>it=map.keySet().iterator();
while(it.hasNext())
{
String key=it.next();
System.out.println("Name: "+key+"Score: "+map.get(key));
}
}
}
'Studynote > Computer Science 12' 카테고리의 다른 글
14.11.11.Tue (0) | 2018.11.05 |
---|---|
14.11.10.mon (0) | 2018.11.05 |
10.14.Tue (0) | 2018.11.05 |
6.추상클래스 (0) | 2018.11.05 |
09-23-Tue 6장 다형성~. (0) | 2018.11.05 |
댓글