zoukankan      html  css  js  c++  java
  • 【Java集合的详细研究6】Java 数组

    Java 语言中提供的数组是用来存储固定大小的同类型元素。

    声明数组变量

    double[] myList; // 首选的方法

    double myList[]; // 效果相同,但不是首选方法

    创建数组

    dataType[] arrayRefVar = new dataType[arraySize];
    dataType[] arrayRefVar = {value0, value1, ..., valuek};

    处理数组

    数组的元素类型和数组的大小都是确定的,所以当处理数组元素时候,我们通常使用基本循环或者 foreach 循环

    public static void main(String[] args) {
          double[] myList = {1.9, 2.9, 3.4, 3.5};
     
          // 打印所有数组元素
          for (int i = 0; i < myList.length; i++) {
             System.out.println(myList[i] + " ");
          }
          // 计算所有元素的总和
          double total = 0;
          for (int i = 0; i < myList.length; i++) {
             total += myList[i];
          }
          System.out.println("Total is " + total);
          // 查找最大元素
          double max = myList[0];
          for (int i = 1; i < myList.length; i++) {
             if (myList[i] > max) max = myList[i];
          }
          System.out.println("Max is " + max);
       }

    foreach 循环

    JDK 1.5 引进了一种新的循环类型,被称为 foreach 循环或者加强型循环,它能在不使用下标的情况下遍历数组。

    public class TestArray {
       public static void main(String[] args) {
          double[] myList = {1.9, 2.9, 3.4, 3.5};
     
          // 打印所有数组元素
          for (double element: myList) {
             System.out.println(element);
          }
       }
    }

    数组作为函数的参数

    数组可以作为参数传递给方法。

    例如,下面的例子就是一个打印 int 数组中元素的方法:

    public static void printArray(int[] array) {
      for (int i = 0; i < array.length; i++) {
        System.out.print(array[i] + " ");
      }
    }

    数组作为函数的返回值

    public static int[] reverse(int[] list) {
      int[] result = new int[list.length];
     
      for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
        result[j] = list[i];
      }
      return result;
    }

    完整代码:

    public class ArrayDemo {
        public static int[] intArray={1,2,3,4}; // 首选的方法
    
        // public static int doubleArray2[]; 效果相同,但不是首选方法。沿用c/c++的写法
    
        public static void main(String[] args) {
            initArray();
            printArray(intArray);
            intArray = reverse(intArray);
            printArray(intArray);
        }
    
        // 创建数组
        public static void initArray() {
            int[] intArray1 = new int[10];
            int[] intArray2={1,2,3,4};
        }
    
        // 数组作为函数的参数
        public static void printArray(int[] array) {
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + " ");
            }
        }
    
        // 数组作为函数的返回值
        public static int[] reverse(int[] list) {
            int[] result = new int[list.length];
    
            for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
                result[j] = list[i];
            }
            return result;
        }
    
    }
  • 相关阅读:
    Aras前端的一些知识
    Eclipse 实用快捷键大全
    Eclipse插件使用links目录的用法
    extjs portal 保存 事件
    NDRS SQL
    [VB]修改注册表让程序开机自动运行
    [C++]数组参数
    [C++]指针类型出参
    [C++]函数返回值
    [VBA]Excel输出utf8编码格式文件 使用WideCharToMultiByte
  • 原文地址:https://www.cnblogs.com/guweiwei/p/6515473.html
Copyright © 2011-2022 走看看