Java中一维数组的学习

一维数组


创建数组

Java语言使用new操作符来创建数组,语法如下:

arrayRefVar = new dataType[arraySize];

上面的语法语句做了两件事:

  • 使用 dataType[arraySize] 创建了一个数组。
  • 把新创建的数组的引用赋值给变量 arrayRefVar。

数组变量的声明,和创建数组可以用一条语句完成,如下所示:

dataType[] arrayRefVar = new dataType[arraySize];

另外,你还可以使用如下的方式创建数组。

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

数组的元素是通过索引访问的。数组索引从 0 开始,所以索引值从 0 到 arrayRefVar.length-1。

实例

下面的语句首先声明了一个数组变量 myList,接着创建了一个包含 10 个 double 类型元素的数组,并且把它的引用赋值给 myList 变量。

public class TestArray {
   public static void main(String[] args) {
      // 数组大小
      int size = 10;
      // 定义数组
      double[] myList = new double[size];
      myList[0] = 5.6;
      myList[1] = 4.5;
      myList[2] = 3.3;
      myList[3] = 13.2;
      myList[4] = 4.0;
      myList[5] = 34.33;
      myList[6] = 34.0;
      myList[7] = 45.45;
      myList[8] = 99.993;
      myList[9] = 11123;
      // 计算所有元素的总和
      double total = 0;
      for (int i = 0; i < size; i++) {
         total += myList[i];
      }
      System.out.println("总和为: " + total);
   }
}

以上实例输出结果为:

总和为: 11367.373

数组的遍历

for循环遍历数组

通常遍历数组都是使用for循环来实现。遍历一维数组很简单,遍历二维数组需要使用双层for循环,通过数组的length属性可获得数组的长度。

public class ForLoopTraversal {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        // 使用for循环遍历数组
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + " ");
        }
        // 输出:1 2 3 4 5
    }
}

for-each循环遍历

Java提供了增强的for循环(也称为"for-each"循环),它简化了数组的遍历。这种循环不需要使用索引来访问数组元素,而是直接遍历数组中的每个元素。

public class EnhancedForLoopTraversal {
    public static void main(String[] args) {
        String[] names = {"Alice", "Bob", "Charlie"};
        // 使用增强for循环遍历数组
        for (String name : names) {
            System.out.print(name + " ");
        }
        // 输出:Alice Bob Charlie
    }
}

while循环遍历

虽然不常见,但也可以使用while循环来遍历数组。需要维护一个索引变量,并在每次迭代后更新它。

public class WhileLoopTraversal {
    public static void main(String[] args) {
        double[] decimals = {1.1, 2.2, 3.3, 4.4, 5.5};
        // 使用while循环遍历数组
        int index = 0;
        while (index < decimals.length) {
            System.out.print(decimals[index] + " ");
            index++;
        }
        // 输出:1.1 2.2 3.3 4.4 5.5
    }
}

do-while循环遍历

do-while循环也可以用于遍历数组。与while循环不同,do-while循环至少执行一次循环体,即使数组为空。

public class DoWhileLoopTraversal {
    public static void main(String[] args) {
        char[] letters = {'A', 'B', 'C', 'D', 'E'};
        // 使用do-while循环遍历数组
        int index = 0;
        do {
            System.out.print(letters[index] + " ");
            index++;
        } while (index < letters.length);
        // 输出:A B C D E
    }
}

数组的反向遍历

有时需要从数组的最后一个元素开始遍历到第一个元素。可以通过从length - 1开始递减索引来实现。

public class ReverseTraversal {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        // 反向遍历数组
        for (int i = numbers.length - 1; i >= 0; i--) {
            System.out.print(numbers[i] + " ");
        }
        // 输出:5 4 3 2 1
    }
}
posted @ 2024-08-08 10:57  BingBing爱化学-04044  阅读(36)  评论(0)    收藏  举报