• 数组应用


    1.数组反转

     1 import java.util.Arrays;
     2 
     3 public class ArrayReverse {
     4     public static void main(String[] args){
     5         int[] arr ={1,2,3,4,5,6,7,8,9};
     6         reverse(arr);
     7     }
     8     public static void reverse(int[] arr){
     9         for(int i=0;i<arr.length/2;i++){
    10             int temp = arr[i];
    11             arr[i] =arr[arr.length-1-i];
    12             arr[arr.length-1-i]=temp;
    13         }
    14         System.out.println(Arrays.toString(arr));
    15     }
    16 }

    2.求最值

     1 class  ZuiZhi
     2  {
     3      public static void main(String[] args) 
     4      {
     5          int[] a={2,92,34,54,28};
     6          minMethod(a);
     7          maxMethod(a);
     8      }
     9      public static void minMethod(int[] a)
    10      {
    11          int min=a[0];//不能初始化为0 数组中可能有负数
    12          for (int i=0;i<a.length;i++ )
    13          {
    14              if(a[i]<min)
    15                  min=a[i];
    16          }
    17          System.out.println("数组内最小的元素是:"+min);
    18      }
    19      public static void maxMethod(int[] a)
    20      {
    21          int max=a[0];//不能初始化为0 数组中可能有负数
    22          for (int i=0;i<a.length;i++ )
    23          {
    24              if(a[i]>max)
    25                  max=a[i];
    26          }
    27          System.out.println("数组内最大的元素是:"+max);
    28      }
    29  
    30  }

    3.折半查找

     1 /*
     2  折半查找:
     3  3 5 7 9 10 14 
     4  min=0 max=length-1 mid=(max+min)/2
     5  */
     6  class BinarySearch 
     7  {
     8      public static void main(String[] args) 
     9      {
    10          int[] a={3,5,7,9,10,14};
    11          int index=binarySearch_1(a,7);
    12          System.out.println(index);
    13          index=binarySearch_2(a,14);
    14          System.out.println(index);    
    15      }
    16      public static int binarySearch_1(int[] a,int key)
    17      {
    18          int min=0,mid,max=a.length-1;
    19          mid=(min+max)/2;
    20          while (key!=a[mid])
    21          {
    22              if(a[mid]>key)
    23              {
    24                  max=mid-1;
    25              }
    26              else  if (a[mid]<key)
    27              {
    28                  min=mid+1;
    29              }
    30              if (max<min)
    31                  return -1;
    32              mid=(min+max)/2;
    33  
    34          }
    35          return mid;
    36      }
    37      public static int binarySearch_2(int[] a,int key)
    38      {
    39          int min=0,max=a.length-1,mid;
    40          while (min<=max)
    41          {
    42              mid =(max+min)>>1;
    43              if(a[mid]>key)
    44              {
    45                  max=mid-1;
    46              }
    47              else  if (a[mid]<key)
    48              {
    49                  min=mid+1;
    50              }
    51              else 
    52                  return mid;
    53          }
    54          return -1;
    55      }
    56      
    57  }

     

  • 相关阅读:
    python : 将txt文件中的数据读为numpy数组或列表
    ROS :为IDE配置环境变量
    ROS 教程之 navigation :在 catkin 环境下创建costmap layer plugin
    ROS 教程之 vision : 用各种摄像头获取图像
    ROS 教程之 network:多台计算机之间网络通信(2)
    MS sql 查询动态表名的方法
    把ocx打包成CAB,并签名
    DataSet 转换成List
    C# 反序列化datetime的处理
    PSP软件开发过程管理
  • 原文地址:https://www.cnblogs.com/malinkang/p/2564497.html
Copyright © 2020-2023  润新知