Good programmers to share practical tutorial large data arrays of large data

Good programmers to share practical tutorial large data arrays of large data

1.5.1 Definition and elements of the array access

An array is a container, a storage container for the specified data type

Precautions:

  1. An array is a fixed length of the container, once the instance is complete, the length can not be modified

Glossary:

  1. Array length: refers to the capacity of the container, this array represents the number of data can be stored
  2. Elements: it refers to the data stored in the array
  3. Index: an index of an element position in the array
  4. Through the array: in order to get each element in the array

Array element access

Accessed by the index, index elements in the array is started from 0

Index array elements: [0, array .length - 1]

note:

While visiting the elements of the array, note the scope of the subject, do not cross-border !!!

Through the array:

  1. Use the subject under way loop through

    int[] array = {1, 2, 3};
    for (int index = 0; index < array.length; index++) {
       System.out.println(array[index]);
    }
  2. Using enhanced for loop

    int[] array = {1, 2, 3};
    for (int ele : array) {
       System.out.println(ele);
    }

1.5.2 Analysis of memory arrays

Common operations 1.5.3 array

1.5.4 Sorting an array

Selection Sort

A fixed index, and then use this index is assigned to each element in turn and a rear element compared subscript

int[] array = {1, 3, 5, 7, 9, 0, 8, 6, 4, 2};
for (int index = 0; index < array.length - 1; index++) {
    for (int compare = index + 1; compare < array.length; compare++) {
        if (array[index] < array[compare]) {
            int temp = array[index];
            array[index] = array[compare];
            array[compare] = temp;
        }
    }
}

Bubble Sort

In turn compare two adjacent elements of the array

int[] array = {1, 3, 5, 7, 9, 0, 8, 6, 4, 2};
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array.length - 1 - i; j++) {
        if (array[j] < array[j + 1]) {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
        }
    } 
}

1.5.5 array element to find

Query from a specified elements in the array appear subscript

  1. Sequential search
  2. Binary search

1.5.6 two-dimensional array

Guess you like

Origin blog.51cto.com/14573321/2457250