package week3;
import java.lang.module.FindException;
import java.util.ArrayList;
//引入了一个类ArrayList
class Value{
private int i;
public void set(int i){ this.i = i;}
public int get(){ return i; }
public String toString(){ return "" + i; } //把i变成字符串
}
public class NoteBook {
// 1、notebook需要有什么功能
// 2、接口设计(业务逻辑)
// add(String note);
// getSize();
// getNote(int index);
// removeNote(int index);
// list();
//容器
//ArrayList<String>意思是ArrayList of String,用来存放String的一个ArrayList
//这种类型叫做范型类,这种范型类是一种容器
//容器类有两个类型:1、容器的类型 2、元素的类型
private ArrayList<String> notes = new ArrayList<>();
public void add(String s){
notes.add(s);
}
public void add(String s, int location){
notes.add(location, s);
}
public int getSize(){
return notes.size();
}
public String getNote(int index){
return notes.get(index);
}
public void removeNote(int index){
notes.remove(index);
}
public String[] list(){
String[] a = new String[notes.size()];
// for(int i = 0; i<notes.size(); i++){
// a[i] = notes.get(i);
// }
notes.toArray(a);
return a;
}
public static void main(String[] args) {
// NoteBook nb = new NoteBook();
// nb.add("first");
// nb.add("second");
// nb.add("third",1);
// System.out.println(nb.getSize());
// System.out.println(nb.getNote(0));
// System.out.println(nb.getNote(1));
// nb.removeNote(1);
// String[] a = nb.list();
// for(String s:a){
// System.out.println(s);
// }
//==============================================================
//对象数组中的每个元素都是对象的管理者而非对象本身
//String[]是对象数组
// int[] ia = new int[10];
// String[] a = new String[10];
// for(int i = 0; i < a.length ; i++){
// a[i] = ""+i;
// }
// System.out.println(ia[0]+2);
// System.out.println(a[0].length());
//
// Value[] b = new Value[10];
// for(int i = 0;i < b.length; i++){
// b[i] = new Value();
// b[i].set(i);
// }
// for(Value v:b ){
// System.out.println(v.get());
// v.set(0);
// }
// for(Value v:b){
// System.out.println(v.get());
// }
//
// //对容器
// ArrayList<String> c = new ArrayList<>();
// c.add("first");
// c.add("second");
// for(String s : a){
// System.out.println(s);
// }
//==============================================================
// //集合容器set
// //集合是无序的,随机排列,不可能输出某个“准确位置”的元素
// Value v = new Value();
// v.set(10);
// System.out.println(v);//输出了week3.Value@50cbc42f,没有什么意义
// //Value中加了 public String toString(){ return "" + i; } 后可输出10
//
//
// ArrayList<String> a = new ArrayList<String>();
// a.add("first");
// a.add("second");
// a.add("first");
// System.out.println(a);//可以直接输出
// /*
// for( String s : a ){
// System.out.println(s);
// }
// */
// System.out.println("-------------");
// HashSet<String> s = new HashSet<String>();
// s.add("first");
// s.add("second");
// s.add("first");
// System.out.println(s);
// /*
// for( String k : s ){
// System.out.println(k);
// }
// */
// //输出结果
// //first
// //second
// //first
// //-------------
// //first
// //second
}
}
package week3;
import java.util.HashMap;
import java.util.Scanner;
public class Coin {
//Hash表
//传统意义上的Hash表,是能以int做值,将数据存放起来的数据结构。Java的Hash表可以以任何实现了hash()函数的类的对象做值来存放对象
//integer是int的包裹类型
private HashMap<Integer,String> coinNames = new HashMap<Integer, String>();
public Coin(){ //构造器
coinNames.put(1,"penny");
coinNames.put(10,"dime");
coinNames.put(25,"quarter");
coinNames.put(50,"half-dolor");
System.out.println(coinNames.keySet().size());
System.out.println(coinNames);
for( Integer k : coinNames.keySet()){
String s = coinNames.get(k);
System.out.println(s);
}
}
public String getName(int amount){
if (coinNames.containsKey(amount))
return coinNames.get(amount);
else
return "NOT FOUND";
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int amount = in.nextInt();
Coin coin = new Coin();
String name = coin.getName(amount);
System.out.println(name);
}
}