第十二周測試
第十二周測試
MySort
註意:研究sort的其他功能,要能改的動代碼,需要答辯
模擬實現Linux下Sort -t : -k 2的功能。
要有偽代碼,產品代碼,測試代碼(註意測試用例的設計)
參考 Sort的實現。提交博客鏈接。
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文檔
Arrays和String單元測試
一.實踐要求
在IDEA中以TDD的方式對String類和Arrays類進行學習
測試相關方法的正常,錯誤和邊界情況
String類
charAt
split
Arrays類
sort
binarySearch
代碼
1.產品代碼
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);
第十二周測試