首页 技术 正文
技术 2022年11月23日
0 收藏 765 点赞 2,513 浏览 2626 个字

20175227张雪莹 2018-2019-2 《Java程序设计》

课下选做作业MySort

要求

  • 注意:研究sort的其他功能,要能改的动代码,需要答辩
  • 模拟实现Linux下Sort -t : -k 2的功能。
  • 要有伪代码,产品代码,测试代码(注意测试用例的设计)
  • 参考 Sort的实现。提交博客链接。

必须答辩才能得分

   import java.util.*;   public class MySort1 {
public static void main(String [] args) {
String [] toSort = {"aaa:10:1:1",
"ccc:30:3:4",
"bbb:50:4:5",
"ddd:20:5:3",
"eee:40:2:20"}; System.out.println("Before sort:");
for (String str: toSort)
System.out.println(str); Arrays.sort(toSort); System.out.println("After sort:");
for( String str : toSort)
System.out.println(str);
}
}

相关知识

  • Linux系统下的Sort功能

    • 功能: 将文本文件内容加以排序,sort可针对文本文件的内容,以行为单位来排序。
    • 参数:
参数 功能说明
-b 忽略每行前面开始出的空格字符。
-c 检查文件是否已经按照顺序排序。
-d 排序时,处理英文字母、数字及空格字符外,忽略其他的字符。
-f 排序时,将小写字母视为大写字母。
-i 排序时,除了040至176之间的ASCII字符外,忽略其他的字符。
-m 将几个排序好的文件进行合并。
-M 将前面3个字母依照月份的缩写进行排序。
-n 依照数值的大小排序。
-o<输出文件> 将排序后的结果存入指定的文件。
-r 以相反的顺序来排序。
-t<分隔字符> 指定排序时所用的栏位分隔字符。
sort -n -k 2 -t'-' date      // -t<分隔字符>   指定排序时所用的栏位分隔字符。  -k  选择以哪个区间进行排序
  • split方法:一个String类的数组以regex传入的分隔符为标准,对字符串进行分隔,使用时分隔符要放在双括号中。

代码实现

  • 伪代码
将字符串数组tosort用“:”分隔,以第二列数值大小为标准从小到大排列:
创建整型数组a,长度与tosort长度相同
调用split函数将tosort数组以:(冒号)为分隔符分成小字符串
将每一个tosort元素的第二例列数值存入数组a中
调用Arrays类的sort函数对a进行排序
输出排序后的结果
  • 产品代码
/**
* @author 20175227 Xueying Zhang
* @date 2019/5/19 11:44.
*/
import java.util.*;public class MySort {
public static void main(String [] args) {
String [] toSort = {
"aaa:10:1:1",
"ccc:30:3:4",
"bbb:50:4:5",
"ddd:20:5:3",
"eee:40:2:20"}; System.out.println("Before sort:");//输出排序前字符串数组
for (String str: toSort)
System.out.println(str); int [] a=new int[toSort.length];
for(int i=0;i<toSort.length;i++){//对toSort每一个元素用split进行划分,并储存进字符串数组list中
String [] list=toSort[i].split(":");
a[i]=Integer.parseInt(list[1]);//将list中第二个元素,即toSort中第二列元素存进a中
}
Arrays.sort(a); System.out.println("After sort:");
for (int i = 0; i < a.length; i++)//对a中每个元素
for (int j = 0; j < toSort.length; j++)//在toSort中每一个元素的第二列中比较
if (a[i] == Integer.parseInt((toSort[j].split(":"))[1]))//如果二者相等
System.out.println(toSort[j]);//就输出该项元素
}
public static int StringTest1(String str){
int a;
String [] list=str.split(":");
a=Integer.parseInt(list[1]);//将list中第二个元素,即toSort中第二列元素存进a中
return a;
}
public static int[] StringTest2(String[] toSort){
int [] a=new int[toSort.length];
for(int i=0;i<toSort.length;i++){//对toSort每一个元素用split进行划分,并储存进字符串数组list中
String [] list=toSort[i].split(":");
a[i]=Integer.parseInt(list[1]);//将list中第二个元素,即toSort中第二列元素存进a中
}
Arrays.sort(a);
return a;
}
}
  • 测试代码
import junit.framework.TestCase;
import org.junit.Test;public class MySortTest extends TestCase {
String [] toSort = {
"aaa:10:1:1",
"ccc:30:3:4",
"bbb:50:4:5",
"ddd:20:5:3",
"eee:40:2:20"};
@Test
public void testl() {
assertEquals(10,MySort.StringTest1("aaa:10:1:1"));
}
public void test2() {
assertEquals(20,MySort.StringTest2(toSort)[1]);
}
}

运行示例

  • 产品代码运行结果

  • 测试结果

代码托管

MySort.java

参考资料

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,428
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,245
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,689
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726