zoukankan      html  css  js  c++  java
  • java 04 数组

    数组

    类型相同,容器

    下标从0开始,以0为基址,查询速度很快

    int arr = new int[5]

    ___________________________________________

    class arraydemo1{
    public static void main(String[] args){

    int[] arr ={7,8,9,44,44};
    System.out.println(arr[1]);

    int[] arr1 ={7,8,9,90,79,77,79};
    System.out.println("max is"+getmax(arr1)+".");
    System.out.println("max is"+getmax1(arr)+".");

    //int arr6 =null;  不存在的数组

    int arr7 =new int[0];  //空数组

    System.out.println("max is"+getmax1(arr7)+".");
    }


    //数组求最大值
    public static int getmax(int [] arr1){
    int max = arr1[0];
    for (int i =1 ; i<arr1.length;i++){
    max = arr1[i]> max ? arr1[i]:max;
    }
    return max;
    }


    //另外一种方法数组求最大值判断是否为空字符串
    public static int getmax1(int[] arr){

    if(arr ==null || arr.length ==0){

    System.out.println("数组无效");

    return -1; //在此返回return,是如果条件成立就不走下面的判断

    }

    int temp = arr[0];// int temp = 0;
    for(int i = 0;i<arr.length;i++){
    if (temp<arr[i]){
    temp =arr[i];
    }
    }
    return temp;
    }
    }

  • 相关阅读:
    WPF
    binding(转)
    C# winForm调用WebService
    如何用vs2010打开vs2013的项目?
    pyqt——布局管理
    pyqt5——对话框
    pyqt5——俄罗斯方块游戏
    pyqt5——事件和信号
    pyQT5——hello world!
    PyQt5 简介
  • 原文地址:https://www.cnblogs.com/simly/p/10008713.html
Copyright © 2011-2022 走看看