import 包的名字.包中的类
当不在同一个包中的时候,但又要用到该包中的类时,可以这样做。也可以 .* ,
这是包含该包中所有的类,但尽量不要这样做,因为可能会有重名的类,这就尴尬
了。每一个包的名字中的 . 代表文件夹的一个层次。
Arraylist
Arratlist<类型(String)> 数组名(notes) = new Arraylist<类型>();
在后面增加元素 notes.add("first");
在任意位置增加元素 notes.add(位置, "firsts");
大小: notes.size();
得到某一位置元素: notes.get(location);
删除某一位置元素: notes.remove(location); 如果正常删除会返回删除的元素 ,否则,会抛出异常
输出结果:[third, four, five, first, second] 顺序不确定
输出结果:
当不在同一个包中的时候,但又要用到该包中的类时,可以这样做。也可以 .* ,
这是包含该包中所有的类,但尽量不要这样做,因为可能会有重名的类,这就尴尬
了。每一个包的名字中的 . 代表文件夹的一个层次。
Arraylist
Arratlist<类型(String)> 数组名(notes) = new Arraylist<类型>();
在后面增加元素 notes.add("first");
在任意位置增加元素 notes.add(位置, "firsts");
大小: notes.size();
得到某一位置元素: notes.get(location);
删除某一位置元素: notes.remove(location); 如果正常删除会返回删除的元素 ,否则,会抛出异常
实现数组元素的拷贝: notes.toArray(拷贝到的数组);
HashSet
HashSet<String> s = new HashSet<String>();
s.add("first");
s.add("second");
s.add("third");
s.add("four");
s.add("five");
System.out.println(s);
输出结果:[third, four, five, first, second] 顺序不确定
HashMap
package test;
import java.util.HashMap;
import java.util.Scanner;
public class Coin {
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);
// 遍历输出 HashMap 中的每一个元素,可以用到如下方法
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);
}
}
输出结果:
1
4
{1=penny, 50=half-dolor, 25=quarter, 10=dime} // System.out.println(coinnames);
penny
half-dolor
quarter
dime
penny