20155201 第十二周课堂实践

一、

测试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

测试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

二、

实现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://www.cnblogs.com/zhuohua/p/6853738.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值