第十二周课上练习

课上实验

sort的学习

import java.util.*;
import java.lang.*;
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"};

        int [] tmp = new int [toSort.length];
        int val;
        String [] s;

        System.out.println("Before sort:");
        for (String i : toSort) {
            System.out.println(i);

        }

        for(int i = 0; i < toSort.length; i++) {
            s = toSort[i].split(":");
            tmp[i] = Integer.parseInt(s[3]);
        }
        Arrays.sort(tmp);

        System.out.println("After sort:");
        for(int i = 0; i < toSort.length; i++)
            for(int j = 0; j < toSort.length; j++){
                s = toSort[j].split(":");
                val = Integer.parseInt(s[3]);
                if(val == tmp[i]) {
                    System.out.println(toSort[j]);
                }
            }
    }
}

test

import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.*;
public class StringArrayDemoTest {
    String s1 = "abcde";
    String s2 = "aaa:bbb:ccc";
    String[] a1 = {"aaa","bbb","ccc"};
    int [] c1 = {2,5,3,4};
    char [] c2 = {'a','b','c','d'};

    @Test
    public void charAt() throws Exception {
        assertEquals('a',s1.charAt(0));
        assertEquals('e',s1.charAt(4));
    }

    @Test
    public void split() throws Exception {
        assertEquals(a1,s2.split(":"));
    }

    @Test
    public void sort() throws Exception{
        Arrays.sort(c1);
        assertEquals(5,c1[3]);
    }

    @Test
    public void binarySearch() throws Exception{
        int c;
        c = Arrays.binarySearch(c2,'c');
        assertEquals(2,c);
    }
}

面临问题:不知道指令的确切意思,通过用idea不断地debug,不断地纠正我的Test类

原文地址:https://www.cnblogs.com/panyinghao/p/6852812.html