20155219课堂实践--第十二周

1.教材代码检查

  • 要求:
    修改教材P98 Score2.java, 让执行结果数组填充是自己的学号:
    提交在IDEA或命令行中运行结查截图,加上学号水印,没学号的不给成绩。
  • 没按时完成原因:没有提前打开IDE,导致时间浪费。以后上课前会提前把IDE打开。
  • 实践代码:

    public class  a {
    public static void main(String[] args) {
int  [] scores=new int [10];
for(int score:scores)
{
    System.out.printf("%2d",score);
}
        System.out.println();
        Arrays.fill(scores,20155219);for(int score1:scores)
        {
            System.out.printf("%9d",score1);
        }
    }
    
  • 运行结果如下图:

image

> (后两题提交成功,在此作为补充学习,码云链接和截图就不贴上来了)<

2、Arrays和String单元测试

-要求:在IDEA中以TDD的方式对String类和Arrays类进行学习,
测试相关方法的正常,错误和边界情况

String类

charAt

split

Arrays类

sort

binarySearch

  • 关于binarySearch方法
    有如下代码:
import java.util.Arrays;
public class dd
{
    public static void main (String []args)
    {
        int a[] = new int[] {1, 3, 4, 6, 8, 9};
        int x1 = Arrays.binarySearch(a, 5);
        int x2 = Arrays.binarySearch(a, 4);
        int x3 = Arrays.binarySearch(a, 0);
        int x4 = Arrays.binarySearch(a, 10);
        System.out.println("x1:" + x1 + ", x2:" + x2);
        System.out.println("x3:" + x3 + ", x4:" + x4);
    }
}

运行结果如下:image
故可知道此方法的作用:1.⑴.binarySearch(object[ ], object key);
如果key++在数组中++,则返回搜索值的索引;否则返回-1或者"-"(插入点)。插入点是索引键将要插入数组的那一点,即第一个大于该键的元素索引。
image
2.⑵.binarySearch(object[ ], int fromIndex, int endIndex, object key);
如果要搜索的元素key在++指定的范围内++,则返回搜索键的索引;否则返回-1或者"-"(插入点)。

3、MySort

模拟实现Linux下Sort -t : -k 2的功能。参考 Sort的实现。

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

        Arrays.sort(toSort);

        System.out.println("After sort:");
        for( String str : toSort)
            System.out.println(str);
    }
}

此为老师给出的例子,关于sort的具体应用:

  • 当使用ArrayList时,有时想获得一个实际的数组,这个数组包含了列表的内容。可以通过调用方法toArray()来实现。
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(new Integer(1));
al.add(new Integer(2));
al.add(new Integer(3));
al.add(new Integer(4));
al.add(new Integer(5));
System.out.println("al中元素:" + al);
//获得数组
Object ia[] = al.toArray();

可以使用ia[i]也可以用al.toArray()[i]来取出数组的值

  • 课堂实践的题目如果使用Collections类的sort()方法该如何实现,写出了如下代码:
 import java.util.*;

          public class c {
              public static List<String> tmp1 = new ArrayList<>();
              public static String[] ss = new String[20];

              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);
                  int[] tmp = new int[toSort.length];

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

                  for (int i = 0; i < toSort.length; i++) {
                      tmp1.add(toSort[i].split(":")[1]);//此处的1表示即将对第二列进行比较
                  }//此时tmp1不是数组
                  Collections.sort(tmp1);
                  for (int i = 0; i < tmp1.size(); i++) {
                      for (int j = 0; j < toSort.length; j++) {
                          if (tmp1.toArray()[i].equals(toSort[j].split(":")[1]))
                              System.out.println(toSort[j]);
                      }
                  }
              }
          }

以此复习了Collections类和Array类和其中的方法。