一、
测试Arrays类中Sort方法和BinSearch方法
import junit.framework.TestCase;
import org.junit.Test;
import java.util.Arrays;
public class ArrayclassTest extends TestCase {
int[] score={1,2,4,5};
@Test
public void testSort(){
Arrays.sort(score);
assertEquals(4,score[2]);
}
@Test
public void testbinarySearch(){
Arrays.sort(score);
assertEquals(0,Arrays.binarySearch(score,1));
assertEquals(-5,Arrays.binarySearch(score,6));
}
}
![1065407-20170514204652582-1496749548.png](https://i-blog.csdnimg.cn/blog_migrate/014c482cc8a10ad13f75378dbd8fd29e.png)
测试String类中的charAt方法和split方法
import junit.framework.TestCase;
import org.junit.Test;
import java.lang.*;
public class StringclassTest extends TestCase {
String buffer="Stri,n,g";
String[] sp=buffer.split(",");
@Test
public void testCharAt(){
assertEquals('S',buffer.charAt(0));
}
@Test
public void testsplit(){
assertEquals("Stri",sp[0]);
}
}
![1065407-20170514204703082-1696630582.png](https://i-blog.csdnimg.cn/blog_migrate/315dae5416ddf993147e4c0be6799c0a.png)
二、
实现Linux内sort -k2功能
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"};
int[] tmp=new int[toSort.length];
String[] sp=new String[toSort.length];
for(int i=0;i<toSort.length;i++){
sp=toSort[i].split(":");
tmp[i]=Integer.parseInt(sp[1]);
}
for(int i=0;i<toSort.length-1;i++){
for(int j=i+1;j<toSort.length;j++){
if (tmp[i] > tmp[j]) {
String temp=toSort[i];
toSort[i]=toSort[j];
toSort[j]=temp;
int tp=tmp[i];
tmp[i]=tmp[j];
tmp[j]=tp;
}
}
}
for(int i=0;i<toSort.length;i++){
System.out.println(toSort[i]);
}
}
}
![1065407-20170514210442519-1939288007.jpg](https://i-blog.csdnimg.cn/blog_migrate/ff36fae71095369313ec4915cf5b75f2.jpeg)