📜  Java-数组

📅  最后修改于: 2020-12-21 01:37:58             🧑  作者: Mango


Java提供了一个数据结构array ,它存储了一个固定大小的相同类型元素的顺序集合。数组用于存储数据的集合,但是将数组视为相同类型的变量的集合通常会更有用。

无需声明单个变量(例如number0,number1,…和number99),而是声明一个数组变量(例如numbers),并使用number [0],numbers [1]和…,numbers [99]表示各个变量。

本教程介绍如何使用索引变量声明数组变量,创建数组和处理数组。

声明数组变量

要在程序中使用数组,必须声明一个变量以引用该数组,并且必须指定该变量可以引用的数组类型。这是声明数组变量的语法-

句法

dataType[] arrayRefVar;   // preferred way.
or
dataType arrayRefVar[];  // works but not preferred way.

-首选样式dataType [] arrayRefVar 。样式dataType arrayRefVar []来自C / C+&plus ;。语言并在Java中被采用以适应C / C++程序员。

以下代码片段是此语法的示例-

double[] myList;   // preferred way.
or
double myList[];   // works but not preferred way.

创建数组

您可以使用具有以下语法的new运算符来创建数组-

句法

arrayRefVar = new dataType[arraySize];

上面的声明做了两件事-

  • 它使用新的dataType [arraySize]创建一个数组。

  • 它将新创建的数组的引用分配给变量arrayRefVar。

可以在一个语句中组合声明一个数组变量,创建一个数组并将该数组的引用分配给该变量,如下所示-

dataType[] arrayRefVar = new dataType[arraySize];

另外,您可以创建数组,如下所示:

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

数组元素通过index访问。数组索引从0开始;也就是说,它们从0开始到arrayRefVar.length-1

以下语句声明一个数组变量myList,创建一个由double类型的10个元素组成的数组,并将其引用分配给myList-

double[] myList = new double[10];

下图表示数组myList。在这里,myList包含十个double值,索引从0到9。

Java数组

处理阵列

在处理数组元素时,我们经常使用for循环或foreach循环,因为数组中的所有元素都是相同的类型,并且数组的大小是已知的。

这是一个完整的示例,显示了如何创建,初始化和处理数组-

public class TestArray {

   public static void main(String[] args) {
      double[] myList = {1.9, 2.9, 3.4, 3.5};

      // Print all the array elements
      for (int i = 0; i < myList.length; i++) {
         System.out.println(myList[i] + " ");
      }
     
      // Summing all elements
      double total = 0;
      for (int i = 0; i < myList.length; i++) {
         total += myList[i];
      }
      System.out.println("Total is " + total);
      
      // Finding the largest element
      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);  
   }
}

这将产生以下结果-

输出

1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5

foreach循环

JDK 1.5引入了一个新的for循环,称为foreach循环或增强的for循环,它使您可以不使用索引变量而顺序遍历整个数组。

以下代码显示数组myList中的所有元素-

public class TestArray {

   public static void main(String[] args) {
      double[] myList = {1.9, 2.9, 3.4, 3.5};

      // Print all the array elements
      for (double element: myList) {
         System.out.println(element);
      }
   }
}

这将产生以下结果-

输出

1.9
2.9
3.4
3.5

将数组传递给方法

正如可以将原始类型值传递给方法一样,也可以将数组传递给方法。例如,以下方法在int数组中显示元素-

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

您可以通过传递数组来调用它。例如,以下语句调用printArray方法以显示3、1、2、6、4和2-

printArray(new int[]{3, 1, 2, 6, 4, 2});

从方法返回数组

方法也可以返回数组。例如,以下方法返回一个与另一个数组相反的数组-

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;
}

数组类

java.util.Arrays类包含各种静态方法,用于对数组进行排序和搜索,比较数组以及填充数组元素。这些方法对于所有原始类型都是重载的。

Sr.No. Method & Description
1

public static int binarySearch(Object[] a, Object key)

Searches the specified array of Object ( Byte, Int , double, etc.) for the specified value using the binary search algorithm. The array must be sorted prior to making this call. This returns index of the search key, if it is contained in the list; otherwise, it returns ( – (insertion point + 1)).

2

public static boolean equals(long[] a, long[] a2)

Returns true if the two specified arrays of longs are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. This returns true if the two arrays are equal. Same method could be used by all other primitive data types (Byte, short, Int, etc.)

3

public static void fill(int[] a, int val)

Assigns the specified int value to each element of the specified array of ints. The same method could be used by all other primitive data types (Byte, short, Int, etc.)

4

public static void sort(Object[] a)

Sorts the specified array of objects into an ascending order, according to the natural ordering of its elements. The same method could be used by all other primitive data types ( Byte, short, Int, etc.)