注意:研究sort的其余功能,要能改的动代码,须要答辩
模拟实现Linux下Sort -t : -k 2的功能。
要有伪代码,产品代码,测试代码(注意测试用例的设计)
参考 Sort的实现。提交博客连接。java
1 import java.util.*; 2 3 public class MySort1 { 4 public static void main(String [] args) { 5 String [] toSort = {"aaa:10:1:1", 6 "ccc:30:3:4", 7 "bbb:50:4:5", 8 "ddd:20:5:3", 9 "eee:40:2:20"}; 10 11 System.out.println("Before sort:"); 12 for (String str: toSort) 13 System.out.println(str); 14 15 Arrays.sort(toSort); 16 17 System.out.println("After sort:"); 18 for( String str : toSort) 19 System.out.println(str); 20 } 21 }
split() :把一个字符串分割成字符串数组
"2:3:4:5".split(":") //将返回["2", "3", "4", "5"]
"hello".split("", 3) //可返回 ["h", "e", "l"]数组
实践代码单元测试
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); String [] s1 = new String[toSort.length]; for (int i = 0; i < toSort.length; i++) { String list[] = toSort[i].split(":"); s1[i] = list[2]; } Arrays.sort(s1); String [] s2 = new String[toSort.length]; for (int i=0; i<s1.length;i++) for (int j=0;j<toSort.length;j++) if( toSort[j].charAt(7) == (s1[i].toCharArray()[0])) s2[i] = toSort[j]; System.out.println("After sort:"); for(String str : s2 ) System.out.println(str); } }
程序截图
学习
1.对于转换字符串数组:调用Integer.parseInt()方法进行转换。
2.int与Integer的区别:
nteger类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时很是有用的其余一些常量和方法。若是须要调用Integer类的方法,查阅API文档测试
在IDEA中以TDD的方式对String类和Arrays类进行学习
测试相关方法的正常,错误和边界状况
String类
charAt
splitthis
Arrays类
sort
binarySearch设计
代码code
1.产品代码blog
public class StringBufferDemo{ StringBuffer buffer = new StringBuffer(); public StringBufferDemo(StringBuffer buffer){ this.buffer = buffer; } public Character charAt(int i){ return buffer.charAt(i); } public int capacity(){ return buffer.capacity(); } public int length(){ return buffer.length(); } public int indexOf(String buf) { return buffer.indexOf(buf); } }
2.测试代码排序
import junit.framework.TestCase; import org.junit.Test; import static org.junit.Assert.*; /** * Created by Administrator on 2017/4/21 0021. */ public class StringBufferDemoTest extends TestCase { StringBuffer a = new StringBuffer("StringBuffer");//测试12个字符(<=16) StringBuffer b = new StringBuffer("StringBufferStringBuffer");//测试24个字符(>16&&<=34) StringBuffer c = new StringBuffer("StringBufferStringBufferStringBuffer");//测试36个字符(>=34) @Test public void testcharAt() throws Exception{ assertEquals('S',a.charAt(0)); assertEquals('g',a.charAt(5)); assertEquals('r',a.charAt(11)); } @Test public void testcapacity() throws Exception{ assertEquals(28,a.capacity()); assertEquals(40,b.capacity()); assertEquals(52,c.capacity()); } @Test public void testlength() throws Exception{ assertEquals(12,a.length()); assertEquals(24,b.length()); assertEquals(36,c.length()); } @Test public void testindexOf() throws Exception{ assertEquals(0,a.indexOf("Str")); assertEquals(5,a.indexOf("gBu")); assertEquals(10,a.indexOf("er")); } }
运行结果
总结
charAt()方法是一个可以用来检索特定索引下的字符的String实例的方法,charAt()方法返回指定索引位置的字符值。索引范围为0~length()-1。
String的charAt的做用是将字符串中第i个位置上的字符(从0开始计数)赋值给n,其用法为
n=string.charAt(i)
String的split的做用是将字符串拆分红为几个字符串,其用法为(将字符串string以:为界限进行拆分,将拆分的几个字符串赋值给字符串数组string1)
string1=string.split(":")
Arrays的sort的做用是将数组中的元素从小到大排序,其用法为(对arr数组进行排序)
Arrays.sort(arr);
Arrays的binarySearch是寻找数组中某个元素所处的位置,其用法为(在arr中寻找数字1,将数字1的位置赋值给n,从0开始计数)
n=Arrays.binarySearch(arr,1);