zoukankan      html  css  js  c++  java
  • 静态方法,Arrays类,二维数组

    一、静态方法

    静态方法属于类的,可以直接使用类名.方法名()调用。

    静态方法的声明

    访问修饰符 static 类型 方法名(参数列表)

    {

             //方法体

    }

    方法的作用:一个程序分解成几个方法,有利于快速调试程序,也有利于提高程序代码的利用率。因为方法是可以多次被调用的,调用次数和调用场合没有限制。

    方法分类:①返回值为(空)void的方法②带具体返回类型的方法③不带参数的方法④带参数的方法

    方法的返回值:如果方法中有返回值,方法中必须使用关键字return返回该值,返回值类型为该方法所定义的返回值类型。

    ①不带返回值的方法

     1 public class BubbleSort{
     2     public static void main(String []argas)
     3     {
     4         int[] array={80,53,12,90,35,22,65,45,82,33};
     5         bubble(array);
     6         print(array);
     7     }
     8     
     9     //冒泡方法
    10     public static void bubble(int[] array)
    11     {
    12         //N个数比较的轮数为N-1次
    13         for(int i=0;i<array.length-1;i++)
    14         {
    15             //每一轮比较的次数为N-1-i次
    16             for(int j=0;j<array.length-i-1;j++)
    17             {
    18                 //比较相邻的2个数,小靠前
    19                 if(array[j]>array[j+1])
    20                 {
    21                     //两个数做交换,通过设置临时变量
    22                     int temp=array[j];
    23                     array[j]=array[j+1];
    24                     array[j+1]=temp;
    25                 }
    26             }
    27         }
    28 
    29     }
    30     
    31     //打印输出方法
    32     public static void print(int[] array)
    33     {
    34         //把排好序的数组输出
    35         for(int i=0;i<array.length;i++)
    36         {
    37             System.out.print(array[i]+",");
    38         }
    39     }
    40 }
    View Code

    ②带返回值的方法

     1 public class BubbleSort{
     2     public static void main(String []argas)
     3     {
     4         int[] array={80,53,12,90,35,22,65,45,82,33};
     5         print(bubble(array));
     6     }
     7     
     8     //冒泡方法
     9     public static int [] bubble(int[] array)
    10     {
    11         //N个数比较的轮数为N-1次
    12         for(int i=0;i<array.length-1;i++)
    13         {
    14             //每一轮比较的次数为N-1-i次
    15             for(int j=0;j<array.length-i-1;j++)
    16             {
    17                 //比较相邻的2个数,小靠前
    18                 if(array[j]>array[j+1])
    19                 {
    20                     //两个数做交换,通过设置临时变量
    21                     int temp=array[j];
    22                     array[j]=array[j+1];
    23                     array[j+1]=temp;
    24                 }
    25             }
    26         }
    27         return array;
    28     }
    29     
    30     //打印输出方法
    31     public static void print(int[] array)
    32     {
    33         //把排好序的数组输出
    34         for(int i=0;i<array.length;i++)
    35         {
    36             System.out.print(array[i]+",");
    37         }
    38     }
    39 }
    View Code

    ③方法复用及不带参数的方法

     1 public class BubbleSort{
     2     public static void main(String []argas)
     3     {
     4         int[] array={80,53,12,90,35,22,65,45,82,33};
     5         show1();
     6         print(array);
     7         show2();
     8         print(bubble(array));
     9         
    10         int[] array1={80,25,12,30,35,22,55,45,82,33};        
    11         show1();
    12         print(array1);
    13         show2();
    14         print(bubble(array1));
    15     }
    16     
    17     public static void show1()
    18     {
    19         System.out.print("排序前:");
    20     }
    21     
    22     public static void show2()
    23     {
    24         System.out.print("排序后:");
    25     }
    26     
    27     //冒泡方法
    28     public static int [] bubble(int[] array)
    29     {
    30         //N个数比较的轮数为N-1次
    31         for(int i=0;i<array.length-1;i++)
    32         {
    33             //每一轮比较的次数为N-1-i次
    34             for(int j=0;j<array.length-i-1;j++)
    35             {
    36                 //比较相邻的2个数,小靠前
    37                 if(array[j]>array[j+1])
    38                 {
    39                     //两个数做交换,通过设置临时变量
    40                     int temp=array[j];
    41                     array[j]=array[j+1];
    42                     array[j+1]=temp;
    43                 }
    44             }
    45         }
    46         return array;
    47     }
    48     
    49     //打印输出方法
    50     public static void print(int[] array)
    51     {
    52         //把排好序的数组输出
    53         for(int i=0;i<array.length;i++)
    54         {
    55             System.out.print(array[i]+",");
    56         }
    57         System.out.println();
    58     }
    59 }
    View Code

    二、Arrays类

    Java的jdk中提供了一个Arrays工具类,此类专门为程序员操作数组提供了很多专有方法,通过方法的调用可以对数组进行赋值,排序,比较,查找元素等功能。

    在jdk的api中搜索arrays可以看到该类的用法

    就举几个例子

     1 import java.util.Arrays;
     2 public class ArraysUtilDemo{
     3     public static void main(String []argas)
     4     {
     5         int[] arraySrc1={6,8,9,16,35,90}; 
     6         //拷贝数组
     7         int[] arrayDes1=Arrays.copyOf(arraySrc1,10);
     8         for(int i=0;i<arrayDes1.length;i++)
     9         {
    10             System.out.print(arrayDes1[i]+" ");
    11         }
    12         
    13         System.out.println("
    **************************");
    14         //拷贝指定数组中的指定范围内的数据
    15         int[] arrayDes2=Arrays.copyOfRange(arraySrc1,2,4);
    16         for(int i=0;i<arrayDes2.length;i++)
    17         {
    18             System.out.print(arrayDes2[i]+" ");
    19         }
    20         
    21         System.out.println("
    **************************");
    22         int[] arraySrc2={8,6,10,16,35,90}; 
    23         boolean flag=Arrays.equals(arraySrc1,arraySrc2);
    24         System.out.print(flag);
    25         
    26         System.out.println("
    **************************");
    27         //数组填充
    28         int[] arrayDes3=new int[10];
    29         Arrays.fill(arrayDes3,10);
    30         for(int i=0;i<arrayDes3.length;i++)
    31         {
    32             System.out.print(arrayDes3[i]+" ");
    33         }
    34         
    35         System.out.println("
    **************************");
    36         //对数组进行排序
    37         Arrays.sort(arraySrc1);
    38         for(int i=0;i<arraySrc1.length;i++)
    39         {
    40             System.out.print(arraySrc1[i]+" ");
    41         }
    42         
    43         System.out.println("
    **************************");
    44         //二分法查找
    45         int x=Arrays.binarySearch(arraySrc1,9);
    46         System.out.print(x);
    47         
    48         System.out.println("
    **************************");
    49         //使用System类的方法来拷贝数组
    50         int[] arrayDes4=new int[10];
    51         System.arraycopy(arraySrc1,0,arrayDes4,2,5);
    52         for(int i=0;i<arrayDes4.length;i++)
    53         {
    54             System.out.print(arrayDes4[i]+" ");
    55         }
    56     }
    57 }
    View Code

    三、二维数组

    ①   可以看成以数组为元素的数组

    ②   Java中二维数组的声明和初始化应按照从高维到低维的顺序排列

    示例

    int[][] arr1=new int[10][];//第二维长度未定

    int[][] arr2=new int[10][20];//第二维长度确定

    ☆虽然这两个数组的创建有区别,但系统为它们分配的堆内存空间大小是一样的。

    对于任何类型的二维数组而言,第一维的大小决定了二维数组对象的大小,因为二维数组的成员是数组引用,数组引用本身大小是固定的。

    初始化二维数组

    ①   静态初始化:int[][] arr={{1,2},{3,4,5},{6,7,8,9}};

    ②   动态初始化:

    String[][] arrStr;                                       //声明

    arrStr=new String[3][];                          //创建,分配内存

    arrStr[0]=new String[2];                       //为高维初始化

    arrStr[1]=new String[3];

    arrStr[2]=new String[4];

    arrStr[0][0]=new String(“abc00”);     //为低维初始化

    arrStr[0][1]=new String(“abc01”);

    arrStr[1][0]=new String(“abc10”);

    arrStr[1][1]=new String(“abc11”);

    arrStr[1][2]=new String(“abc12”);

    arrStr[2][0]=new String(“abc20”);

    arrStr[2][1]=new String(“abc21”);

    arrStr[2][2]=new String(“abc22”);

    arrStr[2][3]=new String(“abc23”);

     1 public class ArrayDemo2{
     2     public static void main(String []args){
     3         int[][] arr=new int[3][];
     4         //每个高维的数组指向一个低维的int数组
     5         arr[0]=new int[2];
     6         arr[1]=new int[3];
     7         arr[2]=new int[4];
     8         
     9         //给低维数组进行赋值
    10         arr[0][0]=1;
    11         arr[0][1]=2;
    12         arr[1][0]=3;
    13         arr[1][1]=4;
    14         arr[1][2]=5;
    15         arr[2][0]=6;
    16         arr[2][1]=7;
    17         arr[2][2]=8;
    18         arr[2][3]=9;
    19         
    20         for(int i=0;i<arr.length;i++)
    21         {
    22             for(int j=0;j<arr[i].length;j++)
    23             {
    24                 System.out.print(arr[i][j]);
    25             }
    26             System.out.println();
    27         }
    28     }
    29 }
    View Code
  • 相关阅读:
    [Docker] Windows 宿主环境下,共享或上传文件到容器的方法
    [Docker]
    [Docker]
    [Docker]
    [Windows]
    [Linux] 树莓派 4B 安装 Ubuntu 19.10 (Eoan Ermine) IOT 版
    [Linux]
    [.Net] 什么是线程安全的并发集合
    [IOT]
    c++库大全
  • 原文地址:https://www.cnblogs.com/wzy330782/p/5269234.html
Copyright © 2011-2022 走看看