Java in similar sort of function

Java in similar sort of function

Preface :

This semester, the beginning of school on Java and C, C ++ as opposed to the study, have been long overdue grasp this, but it has been forgotten, today suddenly think of it, so he looked to find in the JDK API

JDK API in related presentations :

In the JDK lang package has the Comparable and Comparator two interfaces, the role is similar, but slightly different, mainly I learned Comparator in compare () method

JDK API introduced in this way:
Here Insert Picture Description

Implementation

1. If implemented as long as it is ascending in the JDK is the default method may be implemented in ascending function

2. If you want to achieve in descending order , of course, have to carry out the method interface rewrite

DETAILED Visible codes:
wherein the array directly tested using other collections useless

import java.util.*;
/**
 * 自定义集合(类似于结构体)
 * @author 28649
 *
 */
class Num{
	private int x;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}
	
}
/**
 * 自定义排序方式(即:对自定义集合进行排序)
 * @author 28649
 *
 */
class Cmp implements Comparator<Num>{

	public int compare(Num a,Num b) {
		if(a.getX()<b.getX()) {
			return 1;
		}
		else if(a.getX()>b.getX()){
			return -1;
		}
		else {
			return 0;
		}
	}

	
	
}
/**
 * 简单的对数组进行升序
 * @author 28649
 *
 */
/*public class SortTest{
	public static void main(String args[]) {
		
		System.out.println();
		System.out.println("排序后(从小到大),能直接通过默认实现方式实现:");
		Arrays.sort(a);
		for(int i=0;i<10;i++) {
			System.out.print(a[i]+" ");
		}
		System.out.println();
		
		
	}
	
}
*/
/**
 * 对数组进行降序排序,稍微复杂些
 * @author 28649
 *
 */
public class SortTest{
	public static void main(String args[]) {
		Scanner scan=new Scanner(System.in);
		Num a[]=new Num[10];
		for(int i=0;i<10;i++) {
			System.out.print("请输入一个整数:");
			a[i]=new Num();
			int num=scan.nextInt();
			a[i].setX(num);
		}
		System.out.println("排序前:");
		for(int i=0;i<10;i++) {
			System.out.print(a[i].getX()+" ");
		}
		System.out.println();
		System.out.println("排序后(从大到小),需要自己定义实现方式:");
		Arrays.sort(a,0,10,new Cmp());
		for(int i=0;i<10;i++) {
			System.out.print(a[i].getX()+" ");
		}
	}
}

As for what sort of structure, is similar, there is still a paste Code:

Structure are used in Java class implemented mainly presenting two numbers x, Y; following code for x in Descending of the structure

import java.util.*;
/**
 * 自定义集合(类似于结构体)
 * @author 28649
 *
 */
class Num{
	private int x;
	private int y;
	
	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}
	
}
/**
 * 自定义排序方式(即:对自定义集合进行排序)
 * @author 28649
 *
 */
class Cmp implements Comparator<Num>{

	public int compare(Num a,Num b) {
		if(a.getX()<b.getX()) {
			return 1;
		}
		else if(a.getX()>b.getX()){
			return -1;
		}
		else {
			return 0;
		}
	}

	
	
}
/**
 * 简单的对数组进行升序
 * @author 28649
 *
 */
/*public class SortTest{
	public static void main(String args[]) {
		
		System.out.println();
		System.out.println("排序后(从小到大),能直接通过默认实现方式实现:");
		Arrays.sort(a);
		for(int i=0;i<10;i++) {
			System.out.print(a[i]+" ");
		}
		System.out.println();
		
		
	}
	
}
*/
/**
 * 对数组进行降序排序,稍微复杂些
 * @author 28649
 *
 */
public class SortTest{
	public static void main(String args[]) {
		Scanner scan=new Scanner(System.in);
		Num a[]=new Num[10];
		for(int i=0;i<10;i++) {
			System.out.print("请输入整数x,整数y:");
			a[i]=new Num();
			int num1=scan.nextInt();
			int num2=scan.nextInt();
			a[i].setX(num1);
			a[i].setY(num2);
		}
		System.out.println("排序前:");
		for(int i=0;i<10;i++) {
			System.out.println(a[i].getX()+" "+a[i].getY());
		}
		System.out.println();
		System.out.println("排序后(从大到小),需要自己定义实现方式:");
		Arrays.sort(a,0,10,new Cmp());
		for(int i=0;i<10;i++) {
			System.out.println(a[i].getX()+" "+a[i].getY());
		}
	}
}

Published 127 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/boliu147258/article/details/103978017