记事本

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
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值