Java (arrays and common simple algorithms)

Source: Internet
Author: User
Tags array definition

Array

Array: An array is a collection container that stores data of the same data type.

The format of the array definition:
data type [] Variable name = new data type [length];

The benefits of arrays: Assigning a number (index value, corner label, subscript) to each of the data assigned to an array object, the range of index values starts at 0, the maximum is: Length-1.

Local variables: If a variable is declared inside a method (function), then the variable is a local variable.

Member variables: member variables are defined outside the method, within the class.

The most common problems in arrays are:

1.NullPointerException NULL pointer exception reason: The reference type variable does not point to any object, and the property of the object is accessed or the method that called the object.
The 2.ArrayIndexOutOfBoundsException index value is out of bounds. Cause: A non-existent index value was accessed.

How arrays are initialized:

动态初始化:    数据类型[] 变量名 = new 数据类型[长度];静态初始化:      数据类型[] 变量名 = {元素1,元素2.....};

If you have identified the data at the beginning of the program, then it is advisable to use static initialization. If the data is not very clear at the beginning, it is recommended to use dynamic initialization.

classDemo7 { Public Static void Main(string[] args) {//Dynamic initialization        //int[] arr = new INT[10];            //Static initialization        int[] arr = {Ten, -, -, +, -}; for(intindex =0; Index<arr.length; index++) {System. out.Print(arr[index]+","); }/*int[] arr = new INT[50];Scanner Scanner = new Scanner (system.in);for (int i = 0; i< arr.length; i++) {Arr[i] = Scanner.nextint ();        }        */}}
classDemo8 { Public Static void Main(string[] args) {int[] arr = {- A,- -,-5,- -,-4};intMax =Getmax(arr); System. out.println("Maximum value:"+ max); } Public Static int  Getmax(int[] arr) {intmax = arr[0];//For recording the maximum value         for(inti =1; I < arr.length; i++) {if(Arr[i]>max) {//If an element is found to be larger than Max, then the max variable records the element. max = Arr[i]; }        }returnMax }}
Select sort (Direct sort): Use one element to compare the other elements one at a time to match the conditional swap position.
classDemo9 { Public Static void Main(string[] args) {int[] arr = { A,5, -,8,9};//For arrays of 5 elements, it is only possible to find the 4 maximum values to sort.         Selectsort(arr); } Public Static void Selectsort(int[] arr) {//Put the maximum value in the first position.          for(intj =0; J<arr.length-1; J + +) {//control is the number of wheels.              for(inti = j+1; I<arr.length; i++) {//Find out the maximum value                if(Arr[i]>arr[j]) {//Swap location                    inttemp = Arr[i];                    Arr[i] = Arr[j];                ARR[J] = temp; }            }        }//Iterate through the array to see the effectSystem. out.Print("The current element:"); for(inti =0; I<arr.length; i++) {System. out.Print(arr[i]+","); }    }}
Bubble sort: The idea of bubble sorting is to compare the two elements of the next one to each other and match the conditional exchange position.
classDemo10 { Public Static void Main(string[] args) {int[] arr = { A,8, -,5,9};//Maximum index value: 4 Capacity: 5        Bubblesort(arr); } Public Static void Bubblesort(int[] arr) {//Put the maximum value in the last position         for(intj =0; J<arr.length-1; J + +) {//Control wheel number             for(inti =0; I<arr.length-1-j; i++) {//Find a maximum value                //Adjacent element comparison                if(Arr[i]>arr[i+1]){inttemp = Arr[i]; Arr[i] = Arr[i+1]; Arr[i+1] = temp; }            }        }//Iterate through the array to see the effectSystem. out.Print("The current element:"); for(inti =0; I<arr.length; i++) {System. out.Print(arr[i]+","); }            }}
Binary Lookup Method (dichotomy): The use premise must be an ordered array.
classDemo12 { Public Static void Main(string[] args) {int[] arr = { A, -, +, at, Wu};//int index = Searchele (arr,23);        intindex =Halfsearch(Arr, the); System. out.println(The index value where the element is located is: "+ index); } Public Static int Halfsearch(int[] arr,intTarget) {//Define three variables to record the maximum, minimum, and median lookup range index values, respectively        intmax = arr.length-1;intMin =0;intMid = (max+min)/2; while(true){if(Target>arr[mid]) {min = mid+1; }Else if(Target<arr[mid]) {max = mid-1; }Else{//found the element                returnMid }//No case found            if(max<min) {return-1; }//Recalculate intermediate index valuesMid = (Min+max)/2; }        } Public Static int Searchele(int[] arr,intTarget) { for(inti =0; I<arr.length; i++) {if(Arr[i]==target) {returnI }        }return-1; }}
Two-dimensional arrays: two-dimensional arrays are arrays of arrays.

The definition format of a two-dimensional array:

data type [] Variable name = new data type [length 1][length 2];

How to initialize a two-dimensional array:

动态初始化:数据类型[][] 变量名 = new 数据类型[长度1][长度2];  静态初始化:数据类型[][]  变量名 = {{元素1,元素2...},{元素1,元素2...},{元素1,元素2...} ..}
classDemo16 { Public Static void Main(string[] args) {int[] arr = {{Ten, One,9},{ the, A},{ -, *, the, +}};//Traverse a two-dimensional array         for(inti =0; I <arr.length; i++) { for(intj =0; J<arr[i].length; J + +) {System. out.Print(arr[i][j]+","); }//Line breakSystem. out.println(); }    }}

Features of the array:

1. Arrays can only store data of the same data type.
2. The array is assigned an index value to the element stored in the array, the index value starts at 0, and the maximum index value is length-1;
3. Once the array is initialized, the length is fixed.
4. The memory addresses in the array between the elements and the elements are contiguous.

Java (arrays and common simple algorithms)

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.