1. package Demo;
  2. public class NewTitle {
  3. private int id;
  4. private String title;
  5. private String sythor;
  6. public NewTitle(){}
  7. public NewTitle(int id, String title, String sythor) {
  8. super();
  9. this.id = id;
  10. this.title = title;
  11. this.sythor = sythor;
  12. }
  13. public int getId() {
  14. return id;
  15. }
  16. public void setId(int id) {
  17. this.id = id;
  18. }
  19. public String getTitle() {
  20. return title;
  21. }
  22. public void setTitle(String title) {
  23. this.title = title;
  24. }
  25. public String getSythor() {
  26. return sythor;
  27. }
  28. public void setSythor(String sythor) {
  29. this.sythor = sythor;
  30. }
  31. }
  32. import java.util.ArrayList;
  33. public class ArrayListDemo {
  34. public static void main(String[] args) {
  35. NewTitle title1 = new NewTitle(1,"武汉晴天1","admin");
  36. NewTitle title2 = new NewTitle(2,"武汉晴天2","admin");
  37. NewTitle title3 = new NewTitle(3,"武汉晴天3","admin");
  38. NewTitle title4 = new NewTitle(4,"武汉晴天4","admin");
  39. NewTitle title5 = new NewTitle(5,"武汉晴天5","admin");
  40. ArrayList list =new ArrayList();
  41. list.add(title1);
  42. list.add(title2);
  43. list.add(title3);
  44. list.add(title4);
  45. list.add(title5);
  46. System.out.println("新闻标题的总数:"+list.size());
  47. for (int i = 0; i < list.size(); i++) {
  48. NewTitle title=(NewTitle)list.get(i);
  49. System.out.println(title.getId()+"---"+title.getTitle());
  50. }
  51. }
  52. }
  53. import java.util.LinkedList;
  54. public class LinkedDemo {
  55. public static void main(String[] args) {
  56. NewTitle title1 = new NewTitle(1,"武汉晴天1","admin");
  57. NewTitle title2 = new NewTitle(2,"武汉晴天2","admin");
  58. NewTitle title3 = new NewTitle(3,"武汉晴天3","admin");
  59. NewTitle title4 = new NewTitle(4,"武汉晴天4","admin");
  60. NewTitle title5 = new NewTitle(5,"武汉晴天5","admin");
  61. LinkedList list=new LinkedList();
  62. list.addLast(title5);
  63. list.add(title1);
  64. list.add(title2);
  65. list.add(title3);
  66. list.add(1,title4);
  67. list.addFirst(title5);
  68. System.out.println(list.size());
  69. for (int i = 0; i < list.size(); i++) {
  70. NewTitle title=(NewTitle)list.get(i);
  71. System.out.println(title.getId()+"---"+title.getTitle());
  72. }
  73. System.out.println("====================");
  74. list.removeFirst();
  75. for (int i = 0; i < list.size(); i++) {
  76. NewTitle title=(NewTitle)list.get(i);
  77. System.out.println(title.getId()+"---"+title.getTitle());
  78. }
  79. System.out.println("====================");
  80. for (Object obj : list) {
  81. NewTitle title=(NewTitle)(obj);
  82. System.out.println(title.getId()+"---"+title.getTitle());
  83. }
  84. System.out.println("====================");
  85. NewTitle firstNews=(NewTitle)list.getFirst();
  86. System.out.println("头条新闻"+firstNews.getTitle());
  87. NewTitle lastNews=(NewTitle)list.getLast();
  88. System.out.println("头条新闻"+lastNews.getTitle());
  89. //list.clear();
  90. System.out.println(list.size());
  91. System.out.println("====================");
  92. }
  93. }
文档更新时间: 2020-11-04 13:13   作者:mlrs