zoukankan      html  css  js  c++  java
  • day06-Java方法和数组(二)

      今天学习数组的两种常见错误    ArrayIndexOutOfBoundsException     和       NullPointerException。(见day05)

      之后一起做一些数组的练习。

    1.数组遍历(依次输出数组中的每一个元素)

    2.数组元素逆序

     

                                                                     图:元素逆序-奇数

                                                                 图:元素逆序-偶数

    3.对取值范围在1~100的数据集合排序 计数排序 Counting Sort

     图: 计数排序

    4.数组获取最值(获取数组中的最大值或最小值)

    5.数组查表法(根据键盘录入索引,查找对应星期)

    6.数组元素查找(查找指定元素第一次在数组中出现的索引)

      1 import java.util.Arrays;
      2 
      3 /**
      4  * 
      5  * 数组遍历(依次输出数组中的每一个元素)
      6  * 数组元素逆序
      7  * 对取值范围在1~100的数据集合排序  计数排序 Counting Sort
      8  * 数组获取最值(获取数组中的最大值或最小值)
      9  * 数组查表法(根据键盘录入索引,查找对应星期)
     10  * 数组元素查找(查找指定元素第一次在数组中出现的索引)
     11  */
     12 public class Exercise {
     13 
     14   public static void main(String[] args) {
     15     //遍历数组
     16     //int[] arr = new int[10];
     17     //arr[0] = 1;
     18     //arr[1] = 2;
     19     //traverse(arr);
     20 
     21     //数组逆序
     22     //int[] arr1 = {1, 2, 3, 4};
     23     //reverseArray(arr1);
     24     //// Arrays.toString() 得到数组中的所有元素的值
     25     //String s = Arrays.toString(arr1);
     26     //System.out.println(s);
     27 
     28     //排序
     29     int[] arr2 = {5, 100, 100, 20, 1,30, 20};
     30     //sort(arr2);
     31     //System.out.println(Arrays.toString(arr2));
     32 
     33     //int max = findMax(arr2);
     34     //System.out.println(max);
     35 
     36     //System.out.println(findDayOfWeek(7));
     37 
     38     int location = findLocation(arr2, 100);
     39     System.out.println(location);
     40 
     41   }
     42 
     43   //数组遍历(依次输出数组中的每一个元素)
     44   public static void traverse(int[] arr) {
     45     //数组有一个属性length [0, length - 1]
     46     int len = arr.length; //数组长度
     47     System.out.println("len = " + len);
     48     for (int i = 0; i < len; i++) {
     49       System.out.println(arr[i]);
     50     }
     51   }
     52 
     53   public static void reverseArray(int[] arr) {
     54     //完成数组逆序的核心思路:对称位置元素交换位置
     55 
     56     for (int i = 0; i < arr.length / 2; i++) {
     57       int tmp;
     58       tmp = arr[i];
     59       arr[i] = arr[arr.length - 1 - i];
     60       arr[arr.length - 1 - i] = tmp;
     61     }
     62   }
     63 
     64   public static void sort(int[] arr) {
     65 
     66     //1.根据题意创建包含101个元素的数组 countArray
     67     int[] countArray = new int[101];
     68 
     69     //2. 遍历待排序集合,在countArray中,对待排序的数据计数
     70 
     71     for (int i = 0; i < arr.length; i++) {
     72       //得到待排序集合中当前元素的值
     73       int resultIndex = arr[i];
     74       countArray[resultIndex]++;
     75     }
     76 
     77     //3. 在原数组中排序
     78     int index = 0;
     79     for (int i = 0; i < countArray.length; i++) {
     80 
     81       //针对countArray中每一个计数值,把计数值个countArray的数组下标值
     82       for (int j = 0; j < countArray[i]; j++) {
     83           arr[index] = i;
     84           index++;
     85       }
     86     }
     87 
     88   }
     89 
     90   public static int findMax(int[] arr) {
     91     //存储数组中的最大值
     92     int max = arr[0];
     93     for (int i = 1; i < arr.length; i++) {
     94       if(arr[i] > max) {
     95         max = arr[i];
     96       }
     97     }
     98     return max;
     99 
    100   }
    101 
    102   /**
    103    *
    104    * @param index 1星期1 ..  7代表星期日
    105    * @return
    106    */
    107   public static String findDayOfWeek(int index) {
    108     String[] daysOfWeek = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
    109     return daysOfWeek[index - 1];
    110   }
    111 
    112 
    113   public static int findLocation(int[] arr, int value) {
    114 
    115     int resultIndex = -1; //合法位置[0,arr.length - 1]
    116     for (int i = 0; i < arr.length; i++) {
    117       if (arr[i] ==  value) {
    118         resultIndex = i;
    119         break;
    120       }
    121     }
    122     return resultIndex;
    123   }
    124 
    125 
    126 }
  • 相关阅读:
    Designing IP-Based Video Conferencing Systems: Dealing with Lip Synchronization(唇音同步)
    ffmpeg 如何音视频同步
    音视频同步(播放)原理
    您的 PHP 似乎没有安装运行 WordPress 所必需的 MySQL 扩展”处理方法
    ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statemen
    获得H.264视频分辨率的方法
    1080P、720P、4CIF、CIF所需要的理论带宽
    linux mysql 操作命令
    RTP/RTCP的时间同步机制
  • 原文地址:https://www.cnblogs.com/dust2017/p/12705144.html
Copyright © 2011-2022 走看看