Java Arraylist的基本用法
ArrayList是Java常用的泛型容器类,可以存各种基本类型,如Int,String等等
package notebook;
import java.util.ArrayList;
public class NoteBook {
private ArrayList<String> notes = new ArrayList<String>();
public void add(String s){
//不断增加东西,数组不行,因为大小固定了
notes.add(s);
}
public void add(String s,int locate){
notes.add(locate,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<a.length; i++)
{
a[i] = notes.get(i);
//stem.out.println("第"+i+"为"+a[i]);
}
//notes.toArray(a);
return a ;
}
public static void main(String[] args) {
System.out.println("hello world!");
String[] a = new String[7];
a[0] = "aaaaa";
a[1] = "bbbbb";
NoteBook note = new NoteBook();
note.add("aaa");
note.add("bbb");
note.add("ccc");
note.removeNote(1);
String[] b = note.list();
for(String s :b)
System.out.println(s);
}
}
还没有评论,来说两句吧...