Java Learning Summary

Source: Internet
Author: User

1 getting the Project absolute path
String Path ="://"":" + Request.getserverport ()             " / ";
2 Ordering of lists in Java

The first method is to implement the comparable interface in the list object, and the code is as follows:

PublicClass Person implements Comparable<person>{PrivateString name;PrivateInteger order;/** * @return The name*/PublicString GetName () {ReturnName }/** * @param name * The name to set*/PublicvoidSetName (String name) {THIS.name =Name }/** * @return The Order*  /public Integer GetOrder () { return order;} /* * * @param order * The order to set * /public  void Setorder (Integer order) { This.order =< c12> order; } @Override public int compareTo (person arg0) { return this. GetOrder (). CompareTo ( Arg0.getorder ()); } }

Test examples

PublicStaticvoidMain (string[] args) {list<person> ListA =New arraylist<person>(); person P1 =NewPerson (); person P2 =NewPerson (); Person P3 =NewPerson (); P1.setname ("Name1 "); P1.setorder (1 "name2" ); P2.setorder (2 "name3" ); P3.setorder (3for (person P:lista) {system.out.println (P.getname ());}         

The second method is to overload the Collections.sort method with the following code:

PublicClassperson {PrivateString name;PrivateInteger order;/** * @return The name*/PublicString GetName () {ReturnName }/** * @param name * The name to set*/public void setName (String name) { this.name = name;} /* * @return The ORDER * /public  Integer GetOrder () { return order;} /* * * @param order * The order to set * /public  void Setorder (Integer order) { This.ord ER = Order;}}                  
PublicStaticvoidMain (string[] args) {list<person> ListA =New arraylist<person>(); person P1 =NewPerson (); person P2 =NewPerson (); Person P3 =NewPerson (); P1.setname ("Name1"); P1.setorder (1); P2.setname ("Name2"); P2.setorder (2); P3.setname ( "name3" Span style= "color: #000000;" >); P3.setorder (3new comparator<person> {public int compare ( Person arg0, person arg1) {return Arg0.getorder (). CompareTo (Arg1.getorder ()); } }); for (person P:lista) {system.out.println (P.getname ());}         

The results of two executions are sorted in ascending order according to the order of the person object.

The results of the two executions were: Name1name2name3

Java Learning Summary

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.