容器类——记事本

来源:互联网 发布:斯芬克 知乎 编辑:程序博客网 时间:2024/06/05 07:45
package newstudy;import java.util.ArrayList;//类的接口设计public class New {private ArrayList<String> notes = new ArrayList<String>();//容器的使用 <>中的类型可以改变,ArrList 是容器类;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 Delate(int index){notes.remove(index);}//删除某一位置上的数据public String[] Display(){String [] a= new String[notes.size()];int i;for(i=0;i<notes.size();i++)a[i] = notes.get(i);return a;}//输出容器类的字符串,借助字符串数组public static void main(String[] args) {// TODO Auto-generated method stubNew notebook = new New();notebook.Add("hello");notebook.Add("world");notebook.Add("java", 1);//System.out.println(notebook.Getnote(1));//System.out.println(notebook.Getsize());String[] a =notebook.Display();for(String s:a){System.out.println(s);}}}

0 0
原创粉丝点击