Java Arraylist的基本用法

绝地灬酷狼 2022-10-06 02:56 322阅读 0赞

ArrayList是Java常用的泛型容器类,可以存各种基本类型,如Int,String等等

  1. package notebook;
  2. import java.util.ArrayList;
  3. public class NoteBook {
  4. private ArrayList<String> notes = new ArrayList<String>();
  5. public void add(String s){
  6. //不断增加东西,数组不行,因为大小固定了
  7. notes.add(s);
  8. }
  9. public void add(String s,int locate){
  10. notes.add(locate,s);
  11. }
  12. public int getSize(){
  13. return notes.size();
  14. }
  15. public String getNote(int index){
  16. return notes.get(index);
  17. }
  18. public void removeNote(int index){
  19. notes.remove(index);
  20. }
  21. //显示列表
  22. public String[] list(){
  23. String[] a = new String[notes.size()];
  24. for (int i =0 ; i<a.length; i++)
  25. {
  26. a[i] = notes.get(i);
  27. //stem.out.println("第"+i+"为"+a[i]);
  28. }
  29. //notes.toArray(a);
  30. return a ;
  31. }
  32. public static void main(String[] args) {
  33. System.out.println("hello world!");
  34. String[] a = new String[7];
  35. a[0] = "aaaaa";
  36. a[1] = "bbbbb";
  37. NoteBook note = new NoteBook();
  38. note.add("aaa");
  39. note.add("bbb");
  40. note.add("ccc");
  41. note.removeNote(1);
  42. String[] b = note.list();
  43. for(String s :b)
  44. System.out.println(s);
  45. }
  46. }

发表评论

表情:
评论列表 (有 0 条评论,322人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java方法基本

    > 本文主要介绍了什么是方法以及方法的重载,并展示了几个示例,希望能对大家关于这方面的学习有所帮助。 一、方法的基本用法 1.什么是方法 方法就是一个代码片段,类