• Java基础笔记4


    数组

      有一组相同数据类型的数据.

      数据类型[] 数组名称=new 数据类型[长度]; //为该数组开辟空间.

      数据类型[] 数组名称={值,值};

      求数组的长度 数组名称.length; 

      获取数组中的指定位置的元素. 数组名称[下标] 下标从0开始到数组名称.length-1; 

      获取数组中每个元素:通过循环.

    经典算法:

    选择排序和冒泡排序
     1 public class HomeWork{
     2 //选择排序和冒泡排序
     3     public static void main(String[] args) {
     4 //冒泡排序
     5     int[]nums=new int[10];
     6     for (int i = 0; i < args.length-1; i++) {
     7         for (int j = 0; j < args.length-i-1; j++) {
     8             if (nums[j]>nums[j+1]) {
     9                 int temp=nums[j];
    10                 nums[j]=nums[j+1];
    11                 nums[j+1]=temp;
    12             }
    13         }
    14     }
    15 //选择排序
    16     for (int i = 0; i < args.length; i++) {
    17         for (int j = i+1; j < args.length; j++) {
    18             if (nums[i]>nums[j]) {
    19                 int temp=nums[i];
    20                 nums[i]=nums[j];
    21                 nums[j]=temp;
    22             }
    23         }
    24     }
    25     }
    26 }

    顺序查找:

     1 public class 顺序查找 {
     2     public static void main(String[] args) {
     3         int [] arr={23,34,12,35,6,2,56};
     4         Arrays.sort(arr);
     5         int num=56;
     6         for(int i=0;i<arr.length;i++){
     7              if(arr[i]==num){
     8                  System.out.println("该数在数组中存在.");
     9                  break;
    10              }
    11              if(i==arr.length-1){
    12                  System.out.println("该数不存在");
    13              }
    14         }        
    15     }
    16 }

    折半查询(二分查找):前提是排好序的数组

     1 public class 折半查找 {
     2     public static void main(String[] args) {
     3         int [] arr={2,5,6,8,45,56,67,100};
     4         Arrays.sort(arr);//排序
     5         int num=7;
     6         int max=arr.length-1;//最大数的下标
     7         int min=0;//最小数的下标    
     8         while(max>=min){
     9               int mid=(max+min)/2;//中间数的下标
    10               if(num>arr[mid]){
    11                    min=mid+1;
    12               }else if(num<arr[mid]){
    13                    max=mid-1;
    14               }else{
    15                   System.out.println("该数存在.");
    16                   break;
    17               }
    18         }    
    19     }
    20 }

    Java新特性对数组的支持

      可变参和增强循环

     1 public class NewTeDian {
     2     public static void main(String[] args) {
     3         //1.int a=(int)(Math.random()*11+10);//[0,1)   10 ---> 20
     4         int [] arr={1,34,45,67,32};
     5         for(int a:arr){//增强循环 (与下标无关).  1.5以后具有
     6             System.out.print(a+" ");
     7         }
     8         //2.Syntax error, 'for each' statements are only available if source level is 1.5 or greater
     9         String str=fun(2,34,546,6);
    10         System.out.println(str);
    11     }
    12     
    13    public static String fun(int... acc){
    14        for(int a:acc){
    15            System.out.print(a+"-->");
    16        }
    17        
    18        return "我回来了";
    19    }
    20 }
  • 相关阅读:
    mysql安装前的系统准备工作(转)
    mysql多实例的配置(转)
    饼干怪兽和APT攻击
    Linux
    android application简要类(一)
    轨道sql谈话 dbms_monitor
    Android有关Volley使用(十)至Request和Reponse意识
    data URI scheme及其应用
    java在string和int相互转化
    ComponentName意思
  • 原文地址:https://www.cnblogs.com/qq634571685/p/6652513.html
Copyright © 2020-2023  润新知