java

如何对Arraylist进行排序输出?
treeset和treemap通过让类继承comparable方法进行排序,那么我们的数组集合应该如何排序输出呢?
(1)利用Collections.sort(list)方法

package haha;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;
class Pair implements Comparable{
	int left,right;
   Pair(int l,int r){
	   left=l;right=r;
	   }
   public String toString(){
	   return "["+left+","+right+"]";
   }
@Override
public int compareTo(Object arg0) {
	//实现comparable接口的类必须要实现compareto方法,返回正数代表>,返回负数代表<,0代表=
	Pair that=(Pair)arg0;
	if (this.left > that.left)
		return 1;
	else {
		if (this.left < that.left){
			return -1;
		}else {
			if (this.right>that.right)
				return 1;
			else if (this.right < that.right)
				return -1;
		}
		return 0;
	
	}
}
   
}
public class Main {

	public static void main(String[] args) {
	
		//定义一个对象数组P,存放一对值
		Pair p[]={new Pair(0,1),
				new Pair(2,9),
				new Pair(7,0),
				new Pair(8,8),
				new Pair(5,4)};
//		Arrays类中的toString()方法
//		输出排序前数组
//		System.out.println(Arrays.toString(p));
//		Arrays.sort(p);
//		输出排序后数组	
//		System.out.println(Arrays.toString(p));
//	将对象数组P通过aslist方法转化成list集合中的元素	
		ArrayList<Pair> arr=new ArrayList<>(Arrays.asList(p));
//		排序前集合
		System.out.println(arr);
		Collections.sort(arr);
//		排序后
		System.out.println(arr);
//		关于这里为什么能直接输出集合arr,因为该类已经重写了toString()方法
	
	}

}

(2)接口回调时不能省略泛型类型

ArrayList<student> arr=new ArrayList<>();
	List<student> arr=new ArrayList<student>();

(4)所谓的遍历迭代输出,真的有必要吗?

ArrayList<String> arr3=new ArrayList<>();
	arr3.add("niu");arr3.add("meng");
	Collections.sort(arr3);
	System.out.println(arr3);

因为String重写了Object类的toString()方法,所以sysout的时候调用toString方法直接输出,所以如果想要遍历输入自定义类,只需要重写toString方法。
(5) another practice

package haha;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
class student implements Comparable<student>{
	String name;
	int score;
	student(String name,int score){
		this.name=name;
		this.score=score;
	}
	@Override
	public int compareTo(student o) {
		return (this.score-o.score);
	}
	public String toString(){
		return "name:"+name+" score:"+score;
	}
	
}
public class Main {

	public static void main(String[] args) {
	student stu1=new student("niu",100),
			stu2=new student("meng",98),
			stu3=new student("haha",90);
	ArrayList<student> arr=new ArrayList<>();
	arr.add(stu1);arr.add(stu2);arr.add(stu3);
	//向arr中添加对象元素的两种方法
     /*student stu[]={new student("niu",100),
    		 new student("meng",98),
    		 new student("haha",90)	 
    		 };
     ArrayList<student> arr2=new ArrayList<>(Arrays.asList(stu));
     //如何向list中添加一个对象数组?
*/     
	Collections.sort(arr);
	System.out.println(arr);

	
		
	
	}

}

(6)comparable
1.什么是Comparable接口

此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它的自然比较方法 。实现此接口的对象列表(和数组)可以通过 Collections.sort (和 Arrays.sort )进行自动排序。
2.实现什么方法

int compareTo(T o)
比较此对象与指定对象的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。
/*Comparable接口原型为:
* public interface Comparable
* {
* int compareTo(T other);//接口的中方法自动属于public方法
* }
*/
ps:如果类在继承comparable接口时没有指定泛型则compareto的参数则默认为Object类型,所以我们最好还是指定吧。
(7)comparator实现排序
对任意类型集合对象进行整体排序,排序时将此接口的实现传递给Collections.sort方法或者Arrays.sort方法排。

package haha;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
class student  {
	String name;
	int score;
	student(String name,int score){
		this.name=name;
		this.score=score;
	}
	
}
class haha implements Comparator<student>{
	Boolean r;
    haha(Boolean r){
    	this.r=r;
    }
	@Override
	public int compare(student o1, student o2) {
		if (r){
			//如果为正则递增排序
			return o1.score-o2.score;
		}
		else{
			return o2.score-o1.score;
		}
	}
	
}

public class Main {

	public static void main(String[] args) {
	student stu1=new student("niu",100),
			stu2=new student("meng",98),
			stu3=new student("haha",90);
	ArrayList<student> arr=new ArrayList<>();
	arr.add(stu1);arr.add(stu2);arr.add(stu3);
	Collections.sort(arr,new haha(false));
//	sort(List<student> list, Comparator<? super student> c)
//	arr类,haha类均为接口回调,? super student规定比较器的泛型为student的超类(被继承类,包括其本身)
	for(int i=0;i<arr.size();i++){
		student stu=arr.get(i);
		System.out.println(stu.name+","+stu.score);
	}
	}

}

这个是通过新建comparator的实现类,增加一个可以判断顺序还是逆序的构造方法,我们还可以直接用comparator自己的构造方法来实现排序。

Comparator<student> com=new Comparator<student>() {
		
		@Override
		public int compare(student o1, student o2) {
			
			return o1.score-o2.score;
		}
	};
	Collections.sort(arr,com);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值