数组

1.数组的声明以及初始化

 1 class ArrayTest{
 2   //数组的声明
 3   int[] a;
 4   Object b[];
 5     
 6   //数组的初始化
 7   a=new int[9];  //必须指定数组长度
 8   b = new Obejct[]{object1,object2,object3};  //静态初始化
 9   int[] c = {};        
10 }

2.数组的遍历以及求最值

1 class Test{
2   public static void main(String[] args){
3     int[] a = new int[]{1,2,4,2,6,8}
4     for(int i = 0;i<a.length;a++){
5         System.out.println(a[i]);
6     }
7   }  
8 }
 1 public class Test{
 2   public static void main(String[] args){
 3       int a[] = new int[]{32,4,2,45,345,88};
 4       int temp=int[0];
 5       for(int i =0;i<a.length;i++){
 6               if(temp>a[i+1]){ 
 7                        temp = a[i+1];
 8     }
 9     }
10     }    
11      System.out.println(temp);
12 }

3.静态类Arrays

  Arrays.fill(Array,value)用value填充整个数组;

  Arrays.fill(Array,fromIndex,toIndex,value);用value从fromIndex填充到toIndex,包含from但不包含to;

 int[] array = new int[10];
        Arrays.fill(array,9);;
        for(int i = 0 ;i<array.length;i++){
            System.out.println(array[i]);
        }
        Arrays.fill(array,3,6,7);
        for(int i = 0 ;i<array.length;i++){
            System.out.println(array[i]+"---");
        }

   Arrays.sort(Array);串行排序

  Arrays.sort(Array,fromIndex,toIndex);

  Arrays.parrallelSort();并行排序

1      Arrays.sort(array,2,5);
2         for(int i = 0 ;i<array.length;i++){
3             System.out.println(array[i]+"---");
4         }
5         Arrays.parallelSort(array);
6         for(int i = 0 ;i<array.length;i++){
7             System.out.println(array[i]+"---");
8         }

  Arrays.copyOf(Array,int length);复制数组到指定长度多了截取短了补0;

  Arrays.copyRangeOf(Array,fromIndex,toIndex)

4.数组的排序

直接插入排序:

 1 package com.hbcfc.lp;
 2 
 3 public class InsertSort {
 4        public static int[] insertsoort(int[] arr){
 5            int j;
 6             for (int i=1;i<arr.length;i++){//从第二个元素开始比较
 7                 int temp = arr[i];//哨兵
 8                 for(j=i-1;j>=0&&arr[j]>=temp;j--){//前i-1个数组是排好序的子列表
 9                     arr[j+1]=arr[j];//将子列表中的比当前比较的元素大的元素都往后移动一位
10                 }
11                 arr[j+1]=temp;//直到第j个元素比当前比较的元素小跳出循环并且将哨兵插入到合适位置
12             }
13             return arr;
14        }
15 
16     public static void main(String[] args) {
17        int[] aa = InsertSort.insertsoort(new int[]{1,3,5,2,8,4,8});
18        for(int a:aa){
19            System.out.println(a);
20        }
21     }
22 }

冒泡排序:

 1 package com.hbcfc.lp;
 2 
 3 public class BubbleSort {
 4 
 5     public static void main(String[] args) {
 6         int[] arr = new int[]{22,66,24,86,45,79};
 7         int temp;
 8         for(int j = 0;j<arr.length;j++){
 9             for(int i = 0;i<arr.length-j-1;i++){//此处精简每次循环的次数要比上一次少一
10                 if(arr[i]>arr[i+1]){
11                     temp = arr[i];
12                     arr[i] = arr[i+1];
13                     arr[i+1] = temp;
14                 }
15             }
16         }
17 
18         for(int a:arr){
19             System.out.println(a+"--");
20         }
21         
22     }
23 }

 

  

posted @ 2021-05-10 16:37  silencelp  阅读(75)  评论(0)    收藏  举报