码迷,mamicode.com
首页 > 其他好文 > 详细

第十二周测试

时间:2018-05-21 00:06:51      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:image   java   for   capacity   索引   new   实例   ram   extend   

第十二周测试

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);

第十二周测试

标签:image   java   for   capacity   索引   new   实例   ram   extend   

原文地址:https://www.cnblogs.com/l97----/p/9065098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!