package noteBook;
import java.util.ArrayList;//引入一个类ArrayList
public class NoteBook {
// 用来存放String 的ArrayList
// 定义一个成员变量,类型是ArrayList;ArrayList是一个类(范型类,是一个容器)
// 容器类(用来存放对象,要有元素类型<String>和容器类型ArraList); notes本身是一个对象管理者
private ArrayList<String> notes = new ArrayList<String>();
public void add(String s){
notes.add(s);
}
public void add(String s, int location){
notes.add(location, s);//在location位置插入s
}
public int getSize(){
return notes.size(); //ArrayList容器本身知道自己里面存放了多少元素,可以通过size函数就可以得到
}
public String getNote(int index){
return notes.get(index);//ArrayList中,东西是有顺序的,保持添加时的顺序,形成下标索引,与数组下标索引类似
//从0开始
}
public void removeNote(int index) {
notes.remove(index);//index索引,移除index出元素
}
public String[] list() {
String [] a = new String[notes.size()];
notes.toArray(a);//toArray函数,会自动将数组填入
return a;
}
public static void main(String[] args) {
// TODO Auto-generated method
NoteBook nb = new NoteBook();
nb.add("first");
nb.add("second");
nb.add("third",1);//将third插入到1前面
System.out.println(nb.getSize());// 获取记事本中写入了多少个元素
System.out.println(nb.getNote(0));// 读出0位置数据
System.out.println(nb.getNote(1));
nb.removeNote(1);//移除1位置插入的third
}
}