1. The maximum seek array elements, minimum, average, sum and the like -> valid only for the digital type
main idea
By defining a loop variable assignment cycle, then operation of this variable on the line.
Maximum
int a =0;
int[] arr = new int[]{1,5,3,6,7};
for(int i = 0;i < arr.length;i++){
if(arr[i] > a){
a = arr[i];
}
}
Minimum
int a =0;
int[] arr = new int[]{1,5,3,6,7};
for(int i = 0;i < arr.length;i++){
if(arr[i] > a){
a = arr[i];
}
}
Calculate the sum
int a = 0;
int[] arr = new int[]{1,3,4,6,7,3,9};
for(int i = 0;i < arr.length;i++){
a+=arr[i];
}
Calculating the average
int a = 0;
int[] arr = new int[]{1,3,4,6,7,3,9};
for(int i = 0;i < arr.length;i++){
a+=arr[i];
}
int avg = a/arr.length;
2. A copy of the array, reverse, look for ( linear search, a binary search )
Copy array
int[] a = new int[] { 1, 5, 3, 4, 5 };
int[] b = new int[a.length];
// convenient array of a
System.out.println ( " facilitate a an array of values: ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+"\t");
}
System.out.println("");
// array a value replicate array b , and make simple modifications
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
if (i % 2 == 0) {
b[i] = i;
}
}
// convenient array b
System.out.println ( " convenient b array of values: ");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]+"\t");
}
System.out.println("");
// secondary convenient array A , check whether replication is successful
System.out.println ( " secondary facilitate a an array of values: ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+"\t");
}
System.out.println("");
Reverse
Idea: to temporarily store a value through a temporary variable, the first exchange, and finally the end of the temporary value paid
String[] array = new String[] { "aa", "bb", "cc", "dd" };
System.out.println ( " First Convenience: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
System.out.println();
// inversion method
// for (int i = 0, j = array.length - 1; i < j; i++, j--) {
// String a = "";
// a = array[i];
// array[i] = array[j];
// array[j] = a;
// }
// reverse Method Two
for (int i = 0; i < array.length / 2; i++) {
String a = "";
a = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = a;
}
System.out.println ( " Second convenience: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
Linear search
Ideas: an array of convenient cycle compared with the value you are looking for. If the index is equal to return, given unequal described. ( Requires no sorting )
String[] array = new String[]{“aa”,”bb”,”cc”,”dd”,”ee”};
String value = “cc”;
for(int i = 0; i < array.length; i++;){
if(array[i].equals(value)){
System.out.println ( "has been found, the labeled:" + i + ");
break;
}
if(i == array.length){
System.out.println ( "not found");
}
}
Find dichotomy
// binary searching for: requirements for this array must be ordered.
int[] arr3 = new int[]{-99,-54,-2,0,2,33,43,256,999};
boolean isFlag = true;
// int number = 256;
int number = 25;
int head = 0; // first index position
int end = arr3.length - 1; // End index position
while(head <= end){
int middle = (head + end) / 2;
if(arr3[middle] == number){
System.out.println ( " find the specified elements, the index is: " + Middle);
floe = false;
break;
}else if(arr3[middle] > number){
end = middle - 1;
}else{//arr3[middle] < number
head = middle + 1;
}
}
if(isFlag){
System.out.println ( " not looking to play the specified element ");
}
3. Sort the array elements
Bubble Sort
Ideas: by double loop, the outer loop to control the number of loops. Memory cycle is used to compare two adjacent worthy of size, the large value aside large value each time aside after not need to compare, compare the next -i , and so on
int[] array = new int[]{1,5,8,-3,6};
for (int i = 0; i <array.length - 1; i ++) {// outer control cycles
for (int j = 0; j <array.length - 1 - i; j ++) {// memory comparison control number, -i action: in place of the value is allowed in comparison to the
if (array [j] <array [j + 1]) {// compare the size of two adjacent worthy
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
// convenient
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
Selection Sort
int[] array = new int[]{1,5,8,-3,6};
for (int i = 0; i <arr.length - 1; i ++) {// outer control cycles
int index = i; // record the subscript
for (int j = i + 1 ; j <arr.length; j ++) {//// inner layer controls the number of comparisons, -i action: in place of the value is allowed in comparison to the
if (arr [j] <arr [index]) {// compares the acquired minimum worth a small scale
index = j;
}
}
if (index! = i) { // If the minimum index and i are equal, indicating no change
int tmp = arr[i];
arr[i] = arr[index];
arr[index] = tmp;
}
}
// convenient
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
Arrays of using tools
method |
Example |
Explanation |
boolean equals(int[] a,int[] b) |
That Arrays.equals ( array 1, array 2); |
Compare two arrays are equal. A series of overloaded methods. |
String toString(int[] a) |
Arrays.toString ( array name ); |
Output array of information. A series of overloaded methods. |
void fill(int[] a,int val) |
Arrays.fill ( array name , Replace value ); |
Filled to the specified value in the array. A series of overloaded methods. |
void sort(int[] a) |
Arrays.sort ( array name ); |
The array is sorted in ascending order, the bottom is Quicksort. A series of overloaded methods. |
int binarySearch(int[] a,int key) |
Arrays.binarySearch ( array name , query values ) |
An array sorted binary search for the value specified. |