一、ArrayList
Package com.imooc.set; Import of java.util.ArrayList; Import java.util.List; public class ArrayListDemo { public static void main (String [] args) { // the name stored with ArrayList programming language, and outputs. // names include "Java", "C", "C ++", "Go" and "Swift" List List = new new ArrayList (); list.add ( "the Java" ); list.add ( "C" ); List .add ( "C ++" ); list.add ( "Go" ); list.add ( "Swift" ); //The number of elements of the list output System.out.println ( "the number of elements in the list:" + list.size ()); // iterate all programming languages output System.out.println ( "====== ==================== " ); for ( int I = 0; I <list.size (); I ++ ) { of System.out.print (List.get (I) + "" ); } System.out.println (); // remove the C ++ list System.out.println ( "================== ======== " ); // list.remove (2); list.remove (" C ++ " ); System.out.println ( " after removing the elements of c ++ list is: " ); for(int i=0;i<list.size();i++) { System.out.print(list.get(i)+" "); } System.out.println(); } }
Second, the case
- demand
- Add announcement and display
- Insert the announcement at a specified location
- Delete Announcement
- Modify Announcement
- Notice class properties
- Number id
- title title
- Created creator
- Created createTime
- Notice class method
-Construction method
- Method get and set property values of
package com.imooc.set; import java.util.Date; public class Notice { //Notice类,属性:id,title,creator,ctreaterDate private int id; private String title; private String creator; private Date creatTime; //构造方法 public Notice(int id, String title, String creator, Date creatTime) { super(); this.id = id; this.title = title; this.creator = creator; this.creatTime = creatTime; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getCreator() { return creator; } public void setCreator(String creator) { this.creator = creator; } public Date getCreatTime() { return creatTime; } public void setCreatTime(Date creatTime) { this.creatTime = creatTime; } }
Package com.imooc.set; Import of java.util.ArrayList; Import java.util.Date; public class NoticeTest { public static void main (String [] args) { // create an object class Notice generates three announcement Notice notice1 = new new Notice (1, "Welcome to the java world!", "administrator", new new a Date ()); Notice Notice2 = new new Notice (2, "please submit the job on time," "teacher," new new a Date ()); notice3 notice = new new notice (. 3, "attendance notice", "teacher", new new a Date ()); // add announcement ArrayList noticeList =new ArrayList(); noticeList.add(notice1); noticeList.add(notice2); noticeList.add(notice3); //显示公告 System.out.println("公告内容为:"); for(int i=0;i<noticeList.size();i++) { System.out.println(i+1+":"+((Notice)noticeList.get(i)).getTitle()); } } }
3, delete, modify announcement
Package com.imooc.set; Import of java.util.ArrayList; Import java.util.Date; public class NoticeTest { public static void main (String [] args) { // create an object class Notice generates three announcement Notice notice1 = new new Notice (1, "Welcome to the java world!", "administrator", new new a Date ()); Notice Notice2 = new new Notice (2, "please submit the job on time," "teacher," new new a Date ()); notice3 notice = new new notice (. 3, "attendance notice", "teacher", new new a Date ()); // add announcement ArrayList noticeList =new new ; () the ArrayList noticeList.add (notice1); noticeList.add (Notice2); noticeList.add (notice3); // display bulletins System.out.println ( "announcement is:" ); for ( int I = 0 ; I <noticeList.size (); I ++ ) { System.out.println (I + 1'd + ":" + ((Notice) noticeList.get (I)). the getTitle ()); } // then the second Add a location announcement Notice notice4 = new new Notice (4, "online editor can be used", "administrator", new new a Date ()); noticeList.add ( 1 , notice4); // display announcement System.out.println ( "======================" ); System.out.println ( "announcement is:" ); for ( int i 0 =; I <noticeList.size (); I ++ ) { System.out.println (I + 1'd + ":" + ((Notice) noticeList.get (I)). the getTitle ()); } // delete time submit a job announcement noticeList.remove (2 ); // display announcement System.out.println ( "======================" ); System.out .println ( "announcement is:" ); for ( int I = 0; I <noticeList.size (); I ++ ) { the System.out.println(i + 1'd + ":" + . ((Notice) noticeList.get (I)) the getTitle ()); } // modified second announcement title notice4.setTitle ( "the JAVA-line editor You can use the! " ); noticeList.set ( 1 , notice4); // display announcement System.out.println (" ====================== " ); System.out.println ( "announcement is:" ); for ( int I = 0; I <noticeList.size (); I ++ ) { System.out.println (I + 1'd + ":" + (( Notice) noticeList.get (I)) the getTitle ());. } } }