Java Learning Notes--list of available links

Source: Internet
Author: User

NO Linked List Method name Describe
1 public void Add (data type Object) Adding data to a linked list
2

public int size ()

View the number of data in a linked list
3 public boolean isEmpty () To see if the linked list is empty
4 public void Clean () Empty list
5 Public data type get (int index) Returns the data object for the specified index, using the Compare () function method from the custom class
6 Public boolean contains (data type Object) To see if the linked list contains data objects, you need to use the Compare () function method in your custom class
7 public void Remove (data type Object) To delete data objects in a linked list, you need to use the Compare () function method in your custom class
8 Public data type [] ToArray () Convert to an array of data objects

classperson{
String name; intAge ; PublicPerson (String name,intAge ) { //TODO auto-generated Constructor stub This. name=name; This. age=Age ; } Public BooleanCompare (person person) {if(person==NULL)return false; if( This==person)return true; if( This.name==person.name&& This. age==person.age)return true; return false; } Public voidGetInfo () {System.out.println ("Name:" +name+ ", Age:" +Age ); }}classlink{Private classnode{PrivatePerson data; PrivateNode Next; PublicNode (person data) {//TODO auto-generated Constructor stub This. data=data; } /**********************************************/ Public voidAddNode (Node newNode) {if( This. next==NULL) This. next=NewNode; Else { This. Next.addnode (NewNode); } } /*********************************************/ PublicPerson GetNode (intindex) { if(Index==link. This. foot++)//Note: Internal classes can access variables of external classes directly return This. Data; return This. Next.getnode (index); } /*********************************************/ Public BooleanContainsnode (person data) {if( This. Data.compare (data))return true; Else if( This. next==NULL)/*Focus*/ return false; return This. Next.containsnode (data); } Public voidRemoveNode (Node previous,person data) {if( This. Data.compare (data)) Previous.next= This. Next; Else This. Next.removenode ( This, data); } Public voidToarraynode () {Link. This. Retarray[link. This. foot++]= This. Data; if( This. next!=NULL) This. Next.toarraynode (); } /*********************************************/ } PrivateNode Root; Private intcount=0;//number of nodes. Private intFoot//in order to find the relevant location node PrivatePerson [] retarray; Public voidAdd (person data) {Node NewNode=NewNode (data); if(root==NULL) root=NewNode; Else{root.addnode (NewNode); } Count++; } Public intsize () {returncount; } Public BooleanIsEmpty () {return This. count==0; } Public voidClean () {//Java's virtual opportunity to automatically reclaim those allocated spaceroot=NULL; Count=0; } PublicPerson Get (intindex) { if(index>count)return NULL; This. foot=0; return This. Root.getnode (index); } Public Booleancontains (person data) {if(data==NULL)return false; return This. Root.containsnode (data); } Public voidRemove (person data) {if( This. Contains (data)) { if( This. root.data==data) This. root= This. Root.next; Else This. Root.next.removeNode (root, data);//Focus This. count--; } } PublicPerson [] ToArray () {if( This. count==0) return NULL; Retarray=NewPerson[count]; This. foot=0; This. Root.toarraynode (); returnRetarray; }} Public classLinkdemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubLink link=NewLink (); Link.add (NewPerson ("Zhang San", 20)); Link.add (NewPerson ("John Doe", 21)); Link.add (NewPerson ("Harry", 22)); System.out.println ("Size:" +link.size ()); Person[] Per1=Link.toarray (); for(inti=0;i<per1.length;i++) Per1[i].getinfo (); Person TMP=NewPerson ("John Doe", 21); Link.remove (TMP); Person[] Per2=Link.toarray (); for(inti=0;i<per2.length;i++) Per1[i].getinfo (); }}

Java Learning Notes--list of available links

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.