Modification of Java:list Collection

Source: Internet
Author: User
Tags addall int size

The implementation class of the list is ArrayList; Specific code:

List < generic > collection name; Create a list collection

construct method: Public

class name () {
class name a1= collection name. arraylist< generics > ();

}

Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.Iterator;

Import java.util.List; * * Deposit Alternative courses/public class Listtest {public list coursetoselect;//create a new List container for alternate course public listtest () {This
		. Coursetoselect = new ArrayList ();//cannot instantiate the list, but it can instantiate its subclasses, then pass in the list collection}/* Add and view */public void Testadd () { Add the element//First Add method to the collection using 2 Add methods, the last course CR1 = new Course ("1", "Data structure") added to the collection;//Create Course Object Coursetoselect.add (CR1) ;//Call the Add method to pass the course object into/* * After successfully adding the course, the user needs to be printed feedback feedback information needs to be extracted from the information in the call to the PRINT statement////Get method to remove the stored information from the container. 0 for subscript, object in container is obj ECT type, you need to force type conversion Course TEMP = (course) coursetoselect.get (0);//Remove the specified element and force type conversion before assignment System.out.println ("successfully added

		Course: "+ temp.id +". "+ Temp.name);
		The second Add method course Cr2 = new Course ("2", "C language");//Create Course Object Coursetoselect.add (0, CR2);//Use the Add method to add the object containing the course information CR2 to the specified location Course Temp2 = (course) coursetoselect.get (0)//remove element System.out.println at the specified location within the container ("course added successfully: + temp2.id +". "+ Temp2.nam
e);		
Add elements repeatedly, the compiler does not error, the description can be repeated. Coursetoselect.add (0, CR1);//Use the Add method to add an object CR2 containing course information to the specified location course temp0 = (course) coursetoselect.get (0);
		
		Remove the element System.out.println the specified position within the container ("successfully added course: + temp0.id +". "+ Temp0.name); * * Attempt to add an object to a nonexistent element position * * * Course CR3 = new Course ("3", "Eclipse")//* Create Course Object Coursetoselect.add (4, * CR3 //Use the Add method to add the object Cr2 containing course information to the specified location to run the bug,bug reason: array subscript out of bounds///2 AddAll Method Bulk add element//First AddAll method, add the whole collection into, and can only add list class, so You need to convert the course class to List class course[] course = {New Course ("3", "Discrete Math"), New Course ("4", "assembly language")};//create an array containing 2 course objects Cou Rsetoselect.addall (Arrays.aslist (course)), after converting the course array to the list class add in Course Temp3 = (course) coursetoselect.get (2); /remove element Course Temp4 = (course) coursetoselect.get (3)//remove element System.out.println ("Two courses successfully added: + Temp3.id +". "+ Temp

		3.name + ";" + Temp4.id + "." + Temp4.name);
		The second AddAll method course[] Course2 = {New Course ("5", "Higher Mathematics"), New Course ("6", "College English")}; Coursetoselect.addall (2,arrAys.aslist (COURSE2)), add Course2 to the list after conversion to the specified location course TEMP5 = (course) coursetoselect.get (2);//Remove the element course TEMP6 = ( Course) Coursetoselect.get (3);//remove Element System.out.println ("Two courses successfully added: + Temp5.id +". "+ Temp5.name +"; "+ Temp6.id +
	"." + Temp6.name);
		* * * View * Get all the elements in the list * use for loop calendar/public void Testget () {int size= coursetoselect.size ();//Get the length of the list collection
		SYSTEM.OUT.PRINTLN ("There are courses to be selected:");
			for (int i=0;i<size;i++) {Course CR = (course) coursetoselect.get (i);
		System.out.println (Cr.id + "." + Cr.name); }/* * through iterator (iterator) Calendar list * */public void Testterator () {iterator it=coursetoselect.iterator ();//iter
		Ator is also an interface class that can only be instantiated by calling its subclasses iterator System.out.println ("There are courses to be selected (via iterator access)"; while (It.hasnext ()) {//hasnext method to determine if there are any elements in the list, return the Truth Course cr= (course) it.next ();
		M.out.println (Cr.id + "." + Cr.name); }/* * iterators are used to go through the elements of the collection and cannot store any data/* * Access to the collection element through the For each method/public void TeStforeach () {System.out.println ("There are the following courses to choose from:"); For (object Obj:coursetoselect) {//Define an object type variable obj, that is, all elements of the Calendar list Course er= (course) obj;//take out the element, and when a course is stored it is ignored.
		A type conversion System.out.println (Er.id + "." + Er.name) is required for removal.
	}/* * Use the Set method to modify the specified element */public void testmodify () {Coursetoselect.set (4, New Course ("7", "Gross")); * * * Use the Remove method to remove the specified element * The Remove method has two, an incoming variable name, an incoming array subscript, here is only a second example */public void Remove () {//Course cr= (course) c
Oursetoselect.get (4);//Remove, cast type//System.out.println ("I am Course:" +cr.id + "." + cr.name+ "" I Will be deleted ");
Coursetoselect.remove (CR)////////////////////////////////////////////////////
System.out.println ("delete succeeded"); Testforeach ()//Call iterator Output course//Bulk Delete element course[] courses= {(course) Coursetoselect.get (4), (course) Coursetoselect.get (
		
		5)};
		Coursetoselect.removeall (Arrays.aslist (courses));//to the list array System.out.println ("delete succeeded") before passing in;
	Testforeach (); * * * Using the bulk delete element */public void Testtype () {System.out.println ()Try to pass the list to something else ");
	Coursetoselect.add ("Passing Strange things to the list");
		///Create Main method test run code public static void Main (string[] args) {Listtest lt = new Listtest ();
		Lt.testadd ();
		Lt.testmodify ();
		Lt.testforeach ();
Lt.remove ();
Lt.testtype ();
Lt.testget ();
Lt.testterator ();
Lt.testforeach ();
		
	Lt.removeall ();
 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.