因为是使用的泛型,我们并不确定数据类型,

对于数据的比较就不能用平时的大于或者小于。

我们需要比较对象实现Comparable接口,该接口下的compareTo()方法可以用来比大小

定义Sort类:

package com.daleyzou.blog;

/**
* @Author: DaleyZou
* @Description: 定义进行排序都需要哪些方法
* @Date: Created in 20:57 2018/10/29
* @Modified By:
*/
public abstract class Sort<T extends Comparable<T>> {
public abstract void sort(T[] nums); // 排序的方法 public int less(T v, T w){ // 比较大小
return v.compareTo(w);
} public void swap(T[] nums, int i, int j){ // 进行数组值交换
T temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

排序方法使用快速排序:

package com.daleyzou.blog;

import java.util.Arrays;

/**
* @Author: DaleyZou
* @Description: 使用泛型实现对int数组或者String数组进行排序
* 基于快速排序实现
* @Date: Created in 21:07 2018/10/29
* @Modified By:
*/
public class BubbleSort<T extends Comparable<T>> extends Sort<T> {
@Override
public void sort(T[] nums) {
boolean isSorted = false;
for (int i = 0; i < nums.length; i++){
isSorted = true;
for (int j = 1; j < nums.length - i; j++){
if (nums[j].compareTo(nums[j - 1]) < 0){
swap(nums, j, j - 1);
isSorted = false;
}
}
if (isSorted){
break;
}
}
} public static void main(String[] args){
// 验证String类型
String[] strs = new String[]{"123", "1234", "1"};
BubbleSort<String> strSort = new BubbleSort<>();
strSort.sort(strs);
System.out.println("验证String类型:");
Arrays.stream(strs).forEach(System.out::println); // 验证int类型
Integer[] ints = new Integer[]{123,1234,1};
BubbleSort<Integer> intSort = new BubbleSort<>();
intSort.sort(ints);
System.out.println("验证int类型");
Arrays.stream(ints).forEach(System.out::println);
}
}

使用泛型实现对int数组或者String数组进行排序的更多相关文章

  1. int数组转string数组和int数组转string中间用逗号隔开

    //int 数组转string数组 ,,,}; string result=test.Select(i => i.ToString()).ToArray(); //int 数组转 string中 ...

  2. Long数组转String数组

    public static String[] longToString(Long longArray[]) { if (longArray == null || longArray.length &l ...

  3. JAVA将Object数组转换为String数组

    java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; java将Object[ ...

  4. C# List泛型转换,int,string 转字符,转数组

    List转字符串 List<string> List = new List<string>(); string strArray = string.Join(",&q ...

  5. C# 之 将string数组转换到int数组并获取最大最小值

    1.string 数组转换到 int 数组 " }; int[] output = Array.ConvertAll<string, int>(input, delegate(s ...

  6. c#中从string数组转换到int数组

    以前一直有一个数组之间转换的东西,可是忘记了,今天也是找了好久也没有解决,最后用这种方法解决了,分享给大家. " }; int[] output = Array.ConvertAll< ...

  7. C# string数组转int数组(转载)

    C# string数组转int数组   用法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 //字符串数组(源数组) string[] sNums = new[] {"1 ...

  8. C# string数组转int数组

    用法 //字符串数组(源数组) string[] sNums = new[] {"1", "2"}; //整型数组(目标数组) int[] iNums; //转 ...

  9. C# int数组转string字符串

    方式一:通过循环数组拼接的方式: int[] types = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; string result = string.Empty ...

随机推荐

  1. 错误:子进程 已安装 pre-removal 脚本 返回了错误号 1

    解决办法 sudo rm /var/lib/dpkg/info/<package name>.*

  2. pyplot

    错误: 执行 import matplotlib.pyplot 报错 ImportError: No module named _tkinter, please install the python- ...

  3. Murano Weekly Meeting 2016.05.17

    Meeting time: 2016.May.17 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary:   1 ...

  4. Castle.DynamicProxy的使用

    .Net平台AOP技术研究 简单实现 通过继承实现 public interface ICoding { void DoSth(); } public class Coding : ICoding { ...

  5. php字符串函数详解

    nl2br 功能:化换行符为<br> <?php $str = "cat isn't \n dog"; $result = nl2br($str); echo $ ...

  6. matlab 摘要

    matlab中的向量与矩阵 如果定义一个A = [1, 2, 3]; 则A为一个行向量 但是A(:)返回的是一个列向量 关于函数的返回值 在function [a, b, c] = fit_quadr ...

  7. Qt 串口连接

    Qt 串口连接 使用 Qt 开发上位机程序时,经常需要用到串口,在 Qt 中访问串口比较简单,因为 Qt 已经提供了 QSerialPort 和 QSerialPortInfo 这两个类用于访问串口. ...

  8. 【Ionic】---$ionicLoading ion-spinner SVG旋转加载的动画图标

    ionic 加载动作 $ionicLoading $ionicLoading 是 ionic 默认的一个加载交互效果.里面的内容也是可以在模板里面修改. 用法 angular.module('Load ...

  9. 相同datatable合并

  10. Sql 本周当天本期日期转换

    --查询当天: --查询24小时内的: --info为表名,datetime为数据库中的字段值 --查询当天: --查询24小时内的: select * from table where DateDi ...