20155301第十二周java课程程序

20155301第十二周java课程程序

内容一:在IDEA中以TDD的方式对String类和Arrays类进行学习

  • 测试相关方法的正常,错误和边界情况

  • String类

    • charAt
    • split
  • Arrays类

    • sort
    • binarySearch

1.String.charAt(i)的功能是引用字符串下标索引为i的字符,例如:

String string="fengxingck";
 System.out.println(string.charAt(0));

在Junit中的测试:

String buffer="feng:xing:ck";
String[] sp=buffer.split(":");
    @Test
    public void testCharAt(){
        assertEquals('f',buffer.charAt(0));
    }

2.String.split的功能是将字符串按照原有字符串中特定的符号进行分割并返回到字符串数组当中,例如:

string2[]="feng|xing|ck".split("\\|");

在Junit中的测试:

String buffer="feng:xing:ck";
String[] sp=buffer.split(":");
@Test
    public void testsplit(){
        assertEquals("feng",sp[0]);
    }
  • 产品代码截图:

image

  • 测试代码截图:
    image

3.Arrays.sort()的功能是对数组进行升序排序,并返回到原来的数组中,例如:

int a[]={1,3,2,4,5};
for(int score:a){
    System.out.print(score);
        }
        System.out.println();
        Arrays.sort(a);
        for(int score:a){
            System.out.print(score);
        }

测试代码:

int[] score={1,2,4,5};
    @Test
    public void testSort(){
        Arrays.sort(score);
        assertEquals(4,score[2]);
    }

4.Arrays.binarySearch(a[],key)的功能是对数组a中的内容进行查找关键字key,如果有则返回关键字的下标索引,否则返回-1或者"-"(插入点)。插入点是索引键将要插入数组的那一点,即第一个大于该键的元素索引,例如:

for(int i=0;i<a.length;i++){
           System.out.printf("%d的索引为:",a[i]);
           System.out.println(Arrays.binarySearch(a,a[i]));;
       }

测试代码:

@Test
    public void testbinarySearch(){
        Arrays.sort(score);
        assertEquals(0,Arrays.binarySearch(score,1));
        assertEquals(-5,Arrays.binarySearch(score,6));
    }
  • 产品代码截图:

image

  • 测试代码截图:
    image

内容二:模拟实现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 }

解答过程:

1.首先我设置了一个整形数组temp,用于存放第四列的所有数字,其次将字符串数组进行分割,由于各个字符串之间是以“:”的形式隔开的,所以用toSort[i].split("😊;实现分割。
最后,由于第四列数字本身是以字符串的形式存在的,所以用Integer.parseInt将字符串变成数字。

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

2.在将所有的第四列数字存入temp数组后,用sort方法对数组进行排序,之后按照temp数组中的内容在toSort中挑选与temp数值对应的字符串进行输出。

Arrays.sort(temp);
for (int i = 0; i < temp.length; i++) {
     for (int j = 0; j < toSort.length; j++) {
        tra = toSort[j].split(":");
            if (temp[i] == Integer.parseInt(tra[3])){
             System.out.println(toSort[j]);
            continue;}
                       }
                }

-测试代码及截图:

import java.util.*;
           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"};
                   System.out.println("Before sort:");
                   for (String str : toSort)
                       System.out.println(str);
                   System.out.println("After sort:");
                   int[] temp = new int[toSort.length];
                   String tra[] = new String[toSort.length];
                   Arrays.sort(toSort);
                   for (int i = 0; i < toSort.length; i++) {
                       tra = toSort[i].split(":");
                       temp[i] = Integer.parseInt(tra[3]);
                   }
                   Arrays.sort(temp);
                   for (int i = 0; i < temp.length; i++) {
                       for (int j = 0; j < toSort.length; j++) {
                           tra = toSort[j].split(":");
                           if (temp[i] == Integer.parseInt(tra[3])){
                               System.out.println(toSort[j]);
                               continue;}
                       }
                   }
               }
           }

image

码云链接-StringandArrays

码云链接-StringandArraysTest

码云链接-MySort

posted @ 2017-05-10 11:35  滕树晨  阅读(144)  评论(0编辑  收藏  举报