Packageentity; Public classNewtitle {Private intId//ID PrivateString TitleName;//name PrivateString creater;//created by PublicNewtitle () {} PublicNewtitle (intID, String titlename, String creater) { This. ID =ID; This. TitleName =TitleName; This. creater =creater; } Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString Gettitlename () {returnTitleName; } Public voidsettitlename (String titlename) { This. TitleName =TitleName; } PublicString Getcreater () {returncreater; } Public voidSetcreater (String creater) { This. creater =creater; } }
Packagetest;Importjava.util.ArrayList;Importjava.util.List;Importentity. Newtitle; Public classNewtitledemo { Public Static voidMain (string[] args) {//Concrete Implementation Steps//1. Create multiple kinds of news title objectsNewtitle car =NewNewtitle (1, "Car", "admin"); Newtitle Test=NewNewtitle (2, "college entrance", "admin"); //2. Create a Collection object that stores all kinds of news headlinesList newstitlelist =NewArrayList (); //3, in order to add a variety of news headlinesNewstitlelist.add (CAR); Newstitlelist.add (test); //4. Get the total number of news headlinesSystem.out.println ("Number of news headlines:" + newstitlelist.size () + "bar"); //5, according to the location to obtain the corresponding news title, one-to-print the name of each news title, that is, we often say the traversal of the collection object for(inti = 0; I < newstitlelist.size (); i++) {newtitle title=(newtitle) newstitlelist.get (i); System.out.println (i+ 1 + ":" +title.gettitlename ()); } }}
20.ArrayList Get and print news headlines