Some application scenarios for arrays

Source: Internet
Author: User
Tags array length rounds

Tag: is the two-dimensional original traversal ACK operation color Minimum Next

Here's a look at the array of arrays today, with one-dimensional arrays, two-dimensional arrays, and their classic applications. 、

Some notes about one-dimensional arrays yesterday:int[] arr = new int[]{5, 3, 7, 1, 8, 9, 2}; Represents an array with an element type of int, the elements of which are 5,3,7,1,8,9,2, meaning that the length of the array is 7. data type [] Array name = {element 1, element 2, Element 3, ...} ;---not allowed to be defined separatelyApplication of Arrays1. Manipulate the element at the specified position: array name [subscript] 2. Gets the array length: array name. length;3. Iterating over an array,
(1) for (int i = 0; i < arr.length; i++) {            System.out.println (arr[i]);        }   Traversing an array rather than traversing the subscript (2) for (int i:arr) {            //arr[i] + = ten;                    The array can only be traversed but cannot be changed by the values in the array            System.out.println (i);        }  Enhanced for loop, where I sequentially represents each element in the array

  

4. Get the maximum value (Max/min) in the array by one: Define the variable to record the maximum value in the array, and then iterate over the array so that the elements in the array are compared to the maximum, and if the maximum value is greater then this element overrides the original maximum value of two: Define a variable to record the subscript of the maximum value.
4. Get the maximum value in the array (max/min)//Method One: Define the variable to record the maximum value in the array, and then iterate over the array so that the elements in the array are compared to the maximum value, and if the maximum value is greater, the element is overwritten with the original maximum value int max = arr[0];for (int i : arr) {if (Max < i) {max = i;}} SYSTEM.OUT.PRINTLN (max);//Mode two: Define a variable to record the subscript of the maximum value. int flag = 0;for (int i = 1; i < arr.length; i++) {if (Arr[i-1] < Arr[i]) {flag = i;}} System.out.println (Arr[flag]);

  

5. Sorting of the array (n-1) + (n-2) + (n-3) +......+2+1 = (n-1+1) (n-1)/2 = n^2-1/2 N------O (n^2) time complexity: Find a piece of code that is bound to execute in the program The execution time of the code is thought to be Unit 1, the number of times to execute this unit 1 is the time complexity-the time complexity does not take into account the coefficients, generally the highest order, O (n^x), O ((logn) ^x), O (n^x (LOGN) ^y) (The decision is efficiency, Degree of speed) spatial complexity: the extra space required for this program to execute is the space complexity of 3 variables---3 = 3 * n^0, n^0->o (1) (Memory occupied)
Bubble sort for (int i = 1; i < arr.length; i++) {//define a loop to control the number of times per round comparison for (Int J =1; J <= Arr.length-i; j + +) {if (arr[j-1) ; ARR[J]) {int temp = arr[j-1];arr[j-1] = arr[j];arr[j] = temp;}}} String str = arrays.tostring (arr); System.out.println (str);
Just like a daleitai, hold the first one against the back and compare the second with the back. (Replace with small) 5 1 7 0 8 2 6 Comparison of the number of rounds: Length-1 selected subscript for each round: number of rounds-1 subscript for each round comparison: Number of wheels and the last one selects one and the other bit to compare and sort---Select sort time complexity: O (n^2), (n-2) + (n-3) +......+2+1 = (n-1) (n-2)/2=1/2 n^2+1/2 N-O (n^2) space complexity: O (1)
Select sort//control wheel number for (int i = 1; i < arr.length; i++) {//control each round to compare subscript for (int j = i; J < Arr.length; J + +) {if (Arr[i-1] > ARR[J]) {int temp = arr[i-1];arr[i-1] = arr[j];arr[j] = temp;}}} Can only be sorted in ascending order//extension: The underlying uses a quick sort + merge sort//Time complexity: O (NLOGN) arrays.sort (arr); System.out.println (arrays.tostring (arr));

  

expansion: Bubbling sorting and selection sorting are stable sorting algorithms---the stability of the sorting algorithm is based on whether equal elements need to be exchanged when sorting6. Inverse array:---time complexity o (n), Space complexity O (1)
Inverse array//mode one://Time complexity: O (n), spatial complexity: O (n) int[] newArr = new Int[arr.length];for (int i = arr.length-1, j = 0; I >= 0; I--, j+ +) {Newarr[j] = arr[i];}  System.out.println (arrays.tostring (NEWARR));//Mode two: The tail-up exchange//time complexity: O (n), Space complexity O (1) for (int i = 0, j = arr.length-1; I <= J; i++, j--) {int temp = Arr[i];arr[i] = arr[j];arr[j] = temp;} System.out.println (arrays.tostring (arr));

7. Array element Lookup: If the array element is unordered, getting the position of an element can only be compared by traversing the way one by one. (for one) if the array elements are ordered, use the binary to find---space complexity o (1), Time complexity O (logn).     (x =) n->x = log1/2n = log2n/log21/2   ->log2n ->logn (default base is N) (x times per impairment) &N bsp;   2x = N->x = log2n ->logn (default base is N) i2  5  7  8  9  12  35&nb Sp 56  75  76  80   87  352min                    mid                               max            mid      &NBSP;&NBSP;MAX&NBSP;ARR[MID] ==i---mid             arr [Mid] > i           arr[mid] < i 8. Array copy system.arraycopy (array to copy, starting subscript to copy, stored array , the number of the starting subscript to be stored); Arrays.copyof (an array to expand, the size of the expansion);   //copy of array can be used to achieve the replication of the array
/array copy//means copy 4 elements from arr1 subscript 2 into the ARR2 array,//store int[from the ARR2 array's subscript 4] arr1 = {5,1,7,0,8,2,6};int[] arr2 = new Int[5] ; System.arraycopy (arr1,2,arr2,0,4); System.out.println (arrays.tostring (ARR2)); The expansion of the array---the copy of the array---produce a new array, resulting in the array after the expansion and the original array is not the same int[] arr = {3,6,1,7,9}; int[] NewArr = new Int[8]; System.arraycopy (arr,0,newarr,0,arr.length); arr = Newarr;arr = arrays.copyof (arr,8);//This step is equivalent to the previous three steps System.out.println (Arrays.tostring (arr));

  

two-dimensional arraysThe stored element is a one-dimensional array---an array of stored arrays (like a large box with a lot of small boxes) Defining Formats data type [] Array name = new data type [number of one-dimensional array included] [length of each one-dimensional array]; int[[] arr = new int[3][5];Represents the definition of a one-dimensional array that can store 3 integers, each one-dimensional array having 5 integer elements data type [] Array name = new data type [number of one-dimensional array included] [];int[[] arr = new int[5][]; Defines a two-dimensional array that can store 5 integral one-dimensional arrays---must first ensure that one-dimensional array on this one is given the size first, and then the value data type [] Array name = {{Array 1}, {array 2}, {array 3},...};int[[] arr = {{2,4,1},{4,7,2,9},{3},{5,0,6,7,4,3}}; ---the size of the two-dimensional array is 4 NOTE: [] If you follow the data type closely before the variable name, it means that the variable defined later is the type Array of memory Exercise: Yang Hui triangle11 11 2 11 3 3 11 4 6 4 1 Features: The start and end of each line is 1, the remainder of the element is calculated: Arr[i][j] = Arr[i-1][j] + arr[i-1][j-1]; Enter a number to indicate the number of rows, and the first n rows of the output.
Import Java.util.scanner;import Java.util.arrays;public class Demo{public static void Main (string[] args) {Scanner s = new  Scanner (system.in);///define the number of rows int n = s.nextint ();//define a two-dimensional array to store Yang Hui triangle int[][] arr = new int[n][];//iterate through the array, filling the element for (int i = 0; i < N i++) {//To define the size of each one-dimensional array arr[i] = new Int[i + 1];//traverse a one-dimensional array, fill in the element for (int j = 0; J <= I; j + +) {//To determine the first and last elements if (j = = 0 | | J ==i) {AR R[I][J] = 1;} ELSE{ARR[I][J] = Arr[i-1][j] + arr[i-1][j-1];} After the fill is complete, print the filled element System.out.print (arr[i][j]+ "\ t");} System.out.println ();} A loop prints 99 multiplication table for (int i = 1,J = 1;i <= 9; j + +) {//Whichever line you come up with is first printed *system.out.print ("*");//Determine if the last *if is printed (j = = i) {// Line System.out.println ();//number of lines +1i++;//* start from scratch j = 0;}}}

  

Some application scenarios for arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.