Java Arrays array ordering sample sharing _java

Source: Internet
Author: User

The Java API's description of the arrays class is that this class contains various methods for manipulating arrays, such as sorting and searching.

1. Ordering of arrays of basic data types

Description

(1) The sort () in the arrays class uses the "tuned fast sorting Method";

(2) such as int[],double[],char[] arrays of base types, the arrays class simply provides the default ascending order, and does not provide the corresponding descending arrangement.

(3) To sort the arrays of the underlying type in descending order, you need to convert these arrays into corresponding encapsulated class arrays, such as integer[],double[],character[, and so on, to order the arrays of these classes. (In fact, it's better to sort in ascending order than to order it yourself).

Sort by default ascending pairs of groups

Function prototype: static void sort (int[] a) sorts the specified int array in ascending numeric order.

The static void sort (int[] A, int fromindex, int toindex) sorts the specified range of the specified int array in ascending numeric order.

Code instance:

Copy Code code as follows:

Import Java.util.Arrays;
public class Arrayssort_11 {
public static void Main (String args[])
{
Int[] a={1,4,-1,5,0};
Arrays.sort (a);
The contents of the array a[] become { -1,0,1,4,5}
for (int i=0;i<a.length;i++)
System.out.print (a[i]+ "");
}
}

2. Sorting data for a composite data type

Function Prototypes:

(1) Public static<t> void sort (t[] a,comparator c) Sorts the specified array of objects according to the order in which the comparer is produced.

(2) Public static<t> void sort (t[] a,int fromindex,int toindex,comparator c) Sorts the specified range of the specified object array according to the order in which the comparer is produced.

Description: This two sorting algorithm is a "tuned merge sort" algorithm.

Code instance:

Copy Code code as follows:

Package AA;

Import Java.util.Arrays;
Import Java.util.Comparator;

public class ArraySort {
Point[] arr;

ArraySort () {
Arr=new Point[4]; Defines an array of objects arr and allocates storage space
for (int i=0;i<4;i++)
Arr[i]=new Point ();
}

public static void Main (string[] args) {

ArraySort sort=new ArraySort ();
Sort.arr[0].x=2;sort.arr[0].y=1; Initialization, data in an array of objects
sort.arr[1].x=2;sort.arr[1].y=2;
sort.arr[2].x=1;sort.arr[2].y=2;
Sort.arr[3].x=0;sort.arr[3].y=1;

Arrays.sort (Sort.arr, New Mycomprator ()); Sort by using the specified sequencer
for (int i=0;i<4;i++)//Output sort result
System.out.println ("(" +sort.arr[i].x+ "," +sort.arr[i].y+ ")");
}
}

Class point{
int x;
int y;
}

The comparator, the x-coordinate, is sorted from small to large, and X is the same, according to Y from small to large
Class Mycomprator implements Comparator {
public int Compare (object arg0, object arg1) {
Point t1= (Point) arg0;
Point t2= (Point) arg1;
if (t1.x!= t2.x)
Return t1.x>t2.x? 1:-1;
Else
Return t1.y>t2.y? 1:-1;
}
}



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.