Comparable接口实现数组排序
来源:互联网 发布:保健品网络推广策划 编辑:程序博客网 时间:2024/06/05 04:48
Comparable接口可以实现对象数组排序,也就可以对整型数组进行直接排序:
import java.util.*;class Node implements Comparable<Node> {private int value;public Node(int value) {this.value = value;}@Overridepublic int compareTo(Node node) { if (this.value == node.value) { return 0; } else { return this.value > node.value ? 1 : -1; } }public static void sortInt(int[] a) {Node[] node = new Node[a.length];for(int i = 0; i < a.length; i ++) {node[i]=new Node(a[i]);}Arrays.sort(node);for(int i = 0; i < a.length; i ++) {a[i] = node[i].value;}}public static void main(String[] args) {int[] a = {1, 4, 5, 7, 6, 5, 9, 12, 34, 23};sortInt(a);for(int i = 0; i < a.length; i ++) {System.out.print(a[i] + " ");}}}
输出结果:1 4 5 5 6 7 9 12 23 34
0 0
- Comparable接口实现数组排序
- Comparable接口实现对对象数组的排序
- 对数组进行冒泡排序,使用comparable接口实现
- Comparable接口实现对象排序
- 接口Comparable实现List排序
- 对象排序,实现comparable接口
- java中的对象数组排序,--Comparable接口
- C#实现Comparable接口实现排序
- java 对ArrayList排序,实现Comparable接口
- java 对ArrayList排序,实现Comparable接口
- Comparable接口实现自定义类排序
- javaBean实现Comparable接口后排序
- 使用Comparable、Comparator接口实现对对象数组、List集合自定义排序
- java 对象数组的排序(实现Comparable接口)和二分查找
- 实现comparable接口,从写compareTo方法对object数组元素进行排序。
- JAVA中关于对象数组是否可以实现Comparable接口和排序问题
- Comparable接口排序
- 对象数组的排序Comparable接口的使用
- linux ethtool命令
- perl 多线程及信号控制
- JSP
- 加载中动画
- opencv学习_4(opencv基础数据结构 CvPoint & CvSize & CvRect & CvScalar & CvArr & CvMat)
- Comparable接口实现数组排序
- spoj 104 HIGH
- 正则表达式 验证YYYY-MM-DD HH:mm:ss,包含闰年验证(包含世纪年和普通闰年)
- “/usr/bin/ld: cannot find -lopenblas” error in Caffe compilation
- 26. Remove Duplicates from Sorted Array and 80. Remove Duplicates from Sorted Array II
- JAVA学习笔记0004
- perl 多线程
- linux addr2line
- 面试题-华为(16年)-数独-7