zoukankan      html  css  js  c++  java
  • JavaSE编码试题强化练习6

    1.写出选择排序的代码实现,对一个int数组进行排序
    public class TestSelectSort {
      public static void main(String[] args) {
        int [] arr = {87,65,5,5,43,21};
        System.out.print("排序前:[ ");
        for (int i : arr){
          System.out.print(i+" ");
        }
        System.out.println("]");
        selectSort(arr);
        System.out.print("排序后:[ ");
        for (int i : arr){
          System.out.print(i+" ");
        }
        System.out.println("]");
      }
      public static void selectSort(int[] arr){
        for (int i = 0;i < arr.length - 1;i++){
          int minIndex = i;
          for (int j = i;j < arr.length - 1;j++){
            if (arr[minIndex] > arr[j + 1]){
              minIndex = j + 1;
            }
          }
          if (minIndex != i){
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
          }
        }
      }
    }

    运行结果:

     2.使用IO包中的类读取E盘上exam.txt文本文件的内容,每次读取一行内容,将每行作为一个输入放入ArrayList的泛型集合中并将集合中的内容使用加强for进行输出显示。

    public class TestReader {
      public static void main(String[] args) {
        String path = "E:/exam.txt";
        outputMethod(path);
      }
    
      private static void outputMethod(String path) {
        /**
         * 创建集合对象
         */
        List<String> list = new ArrayList<String>();
        /**
         * 创建缓冲区对象
         */
        BufferedReader br= null;
        try {
          br = new BufferedReader(new FileReader(path));
          /**
           * 读取数据每次读取一行
           */
          String line = br.readLine();
          while (line != null){
            list.add(line);
            line = br.readLine();
          }
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }finally {
          try {
            if (br != null){
              br.close();
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
          for (String s : list){
            System.out.println(s);
          }
        }
      }
    }

    文本内容:

    程序运行结果:

     注意:文本内容要以UTF-8格式保存,不然程序读取时会报乱码,如下图(文本以ANSI格式保存的):

     3.编写两个线程,一个线程打印1-52的整数,另一个线程打印字母A-Z。打印顺序为12A34B56C….5152Z。即按照整数和字母的顺序从小到大打印,并且每打印两个整数后,打印一个字母,交替循环打印,直到打印到整数52和字母Z结束。

    /**
     * 打印类
     */
    public class Printer {
      /**
       * 设为1,方便计算3的倍数
       */
      private int index = 1;
    
      /**
       * 打印数字的方法,每打印两个数字,等待打印一个字母
       */
      public synchronized void print(int i){
        while (index % 3 == 0){
          try {
            wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.print(""+i);
        index++;
        notifyAll();
      }
      /**
       * 打印字母,每打印一个字母等待打印两个数字
       */
      public synchronized void print(char c){
        while (index % 3 != 0){
          try {
            wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.print(""+c);
        index++;
        notifyAll();
      }
    }
    /**
     * 打印数字线程
     */
    public class NumberPrinter extends Thread {
      private Printer p;
    
      public NumberPrinter(Printer p) {
        this.p = p;
      }
    
      public void run() {
        for (int i =1;i <= 52;i++){
          p.print(i);
        }
      }
    }
    /**
     * 打印字母线程
     */
    public class LetterPrinter extends Thread {
      private Printer p;
      public LetterPrinter(Printer p){
        this.p = p;
      }
      public void run(){
        for (char c = 'A';c <= 'Z';c++){
          p.print(c);
        }
      }
    }
    public class TestThread {
      public static void main(String[] args) {
        /**
         * 创建打印机对象
         */
        Printer p = new Printer();
        /**
         * 创建线程对象并启动线程
         */
        Thread t1 = new NumberPrinter(p);
        Thread t2 = new LetterPrinter(p);
        t1.start();
        t2.start();
      }
    }

    运行结果:

  • 相关阅读:
    JavaMail
    Web的文件下载
    Web的文件上传
    Tcp 数据对象传输接口对象设计
    m个元素中选取n的组合
    PyQT Quick Start
    版本发布自动触发测试
    plantUML 实操
    Python 版本对比
    Some Python Tricks
  • 原文地址:https://www.cnblogs.com/sinoaccer/p/11965194.html
Copyright © 2011-2022 走看看