首页 > 代码库 > Java容器

Java容器

package notebook;

import java.util.ArrayList;

public class NoteBook {
 private ArrayList<String> notes =new ArrayList<String>();     //创建容器类的对象notes

 public void add(String s)
 {//加入笔记
  notes.add(s);
 }

 public void add(String s,int location)
 {//加入笔记到指定位置location
  notes.add(location, s);
 }

 public int getSize()
 {//返回保存笔记的条数
  return notes.size();
 }

 public String getNote(int index)
 {//返回某条笔记的内容
  return notes.get(index);
 }

 public void removeNote(int index)
 {//删除某条笔记
  notes.remove(1);
 }

 public String[] list()
 {//列出全部笔记内容
  String[] a = new String[notes.size()];
  notes.toArray(a);
  return a;
 }

 public static void main(String[] args) {
  String[] a =new String [2];
  a[0] = "first";
  a[1] = "second";
  NoteBook nb = new NoteBook();
  nb.add("first");
  nb.add("second");
  nb.add("third,1");
  System.out.println(nb.getSize());
  System.out.println(nb.getNote(0));
  System.out.println(nb.getNote(1));
  nb.removeNote(1);
  for(String s :a)
  {
   System.out.println(s);
  }
 }

}
<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

Java容器