"Javase" java generic mechanism

Source: Internet
Author: User

************************************************************************* * * * Original:blog.csdn.net/clark_xu Xu Changliang's Column************************************************************************JavaGeneric mechanism

Generics are the attributes introduced by JavaSE5 , and the nature of generics is the parameterized type. Scenario: During the definition of classes, interfaces, and methods, the data types that are manipulated are specified by the parameters passed in.

For example, in the definition of the ArrayList class, the E in<E> is a generic parameter, and when the object is created, the type can be passed as a parameter, and all e in the definition is replaced with the passed parameter;

arraylist<string> list=new arraylist<string> (); Creates an object with the add parameter type, and the return value of theget method is replaced with a String

Java compilation error if using List.add , because the add parameter is of type String .

All collection types have generic parameters so that when you create a collection object, you can specify the type of object to put into the marriage. Also, theJava compiler makes type checking based on this type.

Generics allow you to create a List collection that specifically stores an object, such as

List<point> pointlist=new linkedlist<point> ();

To create an iterator:

Iterator<point> It=pointlist.iterator ();

5.1Enhanced type forLooping statements

javaSE5 introduces an enhanced for loop that allows you to apply the traversal of arrays and collections.

for (point P:pointlist) {

P.tostring

}

It is understood that each time a point object is fetched from the pointlist and then assigned to loop Traverse p; In fact, when compiling, the enhanced for loop is converted to the iterator pattern, but the delete operation cannot be performed in a loop.

5.2 ListSenior5.2.1 sublistMethod

The list 's sublist method gets the sub list, the sub list and the original list occupy the same storage space, and the operation of the child list will shadow the original List, for example:

Sublist=list.sublist (3,8);

Sublist.set (I,sublist.get (I) *10);

5.2.2 QueueInterface andLinkedListRealize

queue queues are special linear tables that restrict access by:FIFO, which can only be added from one end of an offer, poll The element from the other end

javase provides a queue interface, colleagues use LinkedList to implement the interface,Queue often insert delete operations, such as:

Queue<string> queue=new linkedlist<string> ();

Boolean offer (e e); Adds an object to the end of the queue and returns true if the addition succeeds , for example

Queue.offer ("A"); Queue.offer ("B");

E Peek (); Returns the first element of the team, but does not delete

E poll (); The first element of the popup, which is deleted from the first team and returned

5.2.3 DequeInterface andLinkedListRealize

Deque is a sub-interface of the queue that defines the so-called " double-ended queue ", which is the chalk queue from both ends of the line and the team

The data structure of the stack stack can be implemented if Deque is restricted to queue and queue only from one end.

5.3 ListCommon algorithms5.3.1ComparableInterface

When sorting on an object array or an element in a collection, you need to confirm the comparison logic of the object's elements

public interface comparable<t>{

public int compareTo (T t)

}

The comparable interface defines the CompareTo method, with the following logic:

l f Returns a positive number indicating that this is greater than the Parameter object;

L returns a negative number indicating this is less than the Parameter object

l returns 0 to indicate that this is equal to the Parameter object

Two kinds of application scenarios;

(1) Some of the JDK API calls methods in the compareable interface to understand the size relationship of an object.

(2) Java class implements comparable interface to write this kind of object comparison logic;

Examples of examples:

Point implements the comparable interface, the generic parameter is the point type, making the parameters of the CompareTo method also the point type ,

Public class Point implements comparable<point>{

private int x,int y;

public int compareTo (point p) {

int r1=x*x+y*y;

int r2=p.x*p.x+p.y*p.y;

return R1-R2;

}

}

Note: When writing the CompareTo method, you should pay attention to the consistency of the equals method;

************************************************************************* * * * Original:blog.csdn.net/clark_xu Xu Changliang's Column************************************************************************

"Javase" java generic mechanism

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.