Latest web development tutorials

Java arrays

One array for each programming language for data structures are important, of course, different languages ​​and the realization process of the array are not the same.

Java language provides an array is used to store a fixed size elements of the same type.

You can declare an array of variables, such as the numbers [100] instead of direct statements 100 independent variables number0, number1, ...., number99.

This tutorial will introduce declare Java array creation and initialization, and gives the corresponding code.


Declare an array variable

You must first declare an array variable, you can use the array in your program. Here is the syntax for declaring an array variable:

dataType[] arrayRefVar;   // 首选的方法

或

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

Note: We recommend using dataType [] arrayRefVar style statement declare an array variable. dataType arrayRefVar [] style derived from C / C ++ language, Java is used to make C / C ++ programmers can quickly understand the java language.

Examples

Here is the code example both syntax:

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

或

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

Creating an array

Java language using the new operator to create an array, the syntax is as follows:

arrayRefVar = new dataType[arraySize];

The above syntax statements do two things:

  • First, use the dataType [arraySize] creates an array.
  • Second, the reference to the newly created array is assigned to the variable arrayRefVar.

Declare an array variable, the array can be used to create and complete a statement as follows:

dataType[] arrayRefVar = new dataType[arraySize];

In addition, you can create an array using the following method.

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

Elements of the array is accessed through an index. The array index starts from 0, so the index value from 0 to arrayRefVar.length-1.

Examples

First, the following statement declares an array variable myList, then created a 10 element contains an array of type double, and its reference assigned to myList variable.

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

The above example output is:

总和为: 11367.373

The following image depicts an array myList. Here myList has 10 double array element, its index from 0-9.

java array structure description


Working with arrays

Size and type of the array elements of the array is determined, so when processing array elements, we usually use the basic loop or foreach loop.

Example

This example shows how to create a complete, initialize and manipulate arrays:

public class TestArray {

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

The above examples compiled results are as follows:

1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5

foreach loop

JDK 1.5 introduced a new type of loop, called the foreach loop or enhanced circulation, it is not used under circumstances through the array subscript.

Example

This example is used to display all the elements in the array myList:

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

The above examples compiled results are as follows:

1.9
2.9
3.4
3.5

Array as a function of the parameters

Arrays can be passed as a parameter to the method. For example, the following example is a method int array elements in printing.

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

The following example calls printArray method to print out the 3,1,2,6,4 and 2:

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

Array as the return value of a function

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

Examples of the above result in an array as the return value of the function.


Arrays class

java.util.Arrays class can easily manipulate arrays, it provides all the methods are static. It has the following features:

  • To the array assignment: To fill method.
  • Sorting an array: sort through method, in ascending order.
  • Compare arrays: The equals method is relatively array element values ​​are equal.
  • Find array element: Can sorted array of binary search operation by binarySearch methods.

Specific instructions please see the following table:

No. Method and Description
1 public static int binarySearch (Object [] a, Object key)
Binary search algorithm using an object in a given array search for a given value (Byte, Int, double, etc.). Array must be sorted before calling. If the lookup value is contained in an array, the index of the search key returns; otherwise, (- (insertion point) - 1).
2 public static boolean equals (long [] a, long [] a2)
If the two specified arrays of long equal to one another, it returns true. If both arrays contain the same number of elements, and two arrays of all corresponding pairs of elements are equal, the two arrays are considered equal. In other words, if the two arrays in the same order that contains the same elements in the two arrays are equal. The same applies to all other basic data types (Byte, short, Int, etc.).
3 public static void fill (int [] a, int val)
Int value Assigns the specified int array of the specified range of each element. The same applies to all other basic data types (Byte, short, Int, etc.).
4 public static void sort (Object [] a)
The specified array of objects into ascending order according to the natural ordering of its elements. The same applies to all other basic data types (Byte, short, Int, etc.).