array- The originator of the data structure, can say that the array can almost represent all the data structure, in each programming language, the array is an important data structure, of course, each language of the implementation and processing of the group is not the same, but the essence is used to store data structure, Here we take the Java language as an example to detail the use of arrays in the Java language.
1. Introduction to Java Arrays
In Java, arrays are collections of the same data type, and note that only the same data type (except for an array of type Object) can be stored .
1.1, the declaration of the Array
The first way:
Data type [] new data type [ array length ];
The second way:
Data type [] array name = {array element 1, array element 2, array element 3 ...}
This way the array is declared and the array's elements are given directly, and the size of the array is determined by the number of array elements given.
// DECLARE array 1, declare a length of 3, can only hold int type data int [] new int [3]; // declare array 2, declare an array element of type int. int [] myArray2 = {n/a};
②, accessing array elements, and assigning values to array elements
Group is the existence of subscript index, we can get the element at the specified position by subscript, the array subscript starting from 0, that is, the subscript 0 corresponds to the 1th element in the array, it is convenient to access the elements in the group.
Using the second method, we declare an array, and we also initialize the array with the initialization assignment.
// Declare an array that declares a length of 3 and can only hold data of type int int [] new int[3]; // Assign a value of 1 to the first element of the MyArray Myarray[0] = 1; // access the first element of the MyArray System.out.println (Myarray[0]);
Above the MyArray array, we can only assign a value of three elements, that is, subscript from 0 to 2, if you visit myarray[3], then the group subscript out of bounds exception .
③, array traversal
The array has a length property, which is the size of the record array, and we can use the length property to iterate through the array.
// declare array 2, declare an array of array elements of type int int [] myArray2 = {N/a }; for (int i = 0; i < myarray2.length; i++) { System.out.println (myarray2[i]); }
2. Implement data structure with class encapsulation array
We know that a data structure must have the following basic functions:
①, how to insert a new data item
②, how to find a particular data item
③, how to delete a specific data item
④, how to iterate over each item of data for display or other action
Now that we've learned about the simple use of arrays, we're wrapping an array with the idea of a class that implements the four basic functions above:
Ps: Assuming that the operator does not add duplicate elements, there is no consideration of repeating elements, if you add duplicate elements, the subsequent search, delete, modify and so on will only be valid for the first occurrence of the element .
Public classMyArray {Private int[] intarray; // define an array Private intElems // define the actual effective length of the array Private intLength // define the maximum length of the array // default construct an array of length (max. 50) PublicMyArray () {Elems= 0; Length= 50; Intarray=New int[length]; } // constructor, initializes an array of length PublicMyArray (intlength) {Elems= 0; This. length =length; Intarray=New int[length]; } // Gets the effective length of the array Public intGetSize () {returnElems; } // traverse display element Public voiddisplay () { for(inti = 0; i < Elems; i++) {System.out.print (Intarray[i]+ " "); } System.out.println (); } /** * add element * * @param value, assuming the operator does not add duplicate elements, if there are duplicate elements will have an effect on subsequent operations. * @return Add successfully returns TRUE, the added element exceeds the range returned false * / Public BooleanAddintvalue) { if(Elems = =length) { return false; } Else{Intarray[elems]=value; Elems++; } return true; } /** * Get element according to subscript * * @param i * @return find subscript value within the range of the array subscript, return the element represented by the subscript to find the subscript super Array subscript Valid value, prompt to access subscript out of bounds * / Public intGetinti) {if(I < 0 | | i >Elems) {System.out.println ("access subscript out of bounds"); } returnIntarray[i]; } /** * Find element * * @param searchvalue * @return Find the element that returns the subscript value if it exists, returns if it does not exist -1 * / Public intFindintsearchvalue) { inti; for(i = 0; i < Elems; i++) { if(Intarray[i] = =searchvalue) { Break; } } if(i = =Elems) { return-1; } returni; } /** * Delete element * * @param value * @return returns False if the value to be removed does not exist, otherwise returns true, deleting Success * / Public BooleanDeleteintvalue) { intK =find (value); if(k = =-1) { return false; } Else { if(k = = Elems-1) {Elems--; } Else { for(inti = k; i < elems-1; i++) {Intarray[i]= Intarray[i + 1]; } elems--; } return true; } } /** * Modify Data * * @param oldvalue Original value * @param newvalue New Value * @return The modification succeeded returns true, the modification failed to return false * / Public BooleanModifyintOldValue,intnewvalue) { inti =find (OldValue); if(i = =-1) {System.out.println ("data that needs to be modified does not exist"); return false; } Else{Intarray[i]=NewValue; return true; } }}
Test:
Public classTest { Public Static voidMain (string[] args) {//Create a custom encapsulated array structure with an array size of 4MyArray array =NewMyArray (4); //add 4 elements, respectively, 1,2,3,4Array.add (1); Array.add (2); Array.add (3); Array.add (4); //displaying array elementsArray.display (); //according to the element of subscript 0 inti = array.get (0); System.out.println (i); //Remove 4 ElementsArray.delete (4); //Modify element 3 toArray.modify (3, 33); Array.display (); }}
Printing results are:
3, analysis of the limitations of the array
Through the above code, we find that the array can complete all the functions of a data structure , and it is not difficult to implement, since the array can do all the work, So why not use it for all data storage in the real world? There must be a reason for that.
3.1, the analysis of the limitations of the array:
①, Insert Fast , for unordered arrays, the arrays we implement above are unordered, that is, the elements are not arranged in a large or small or a specific order, but in the order in which they are inserted. A new element in an unordered array is simply a matter of adding an element at the end of the array, but the ordered array is not necessarily, it needs to be inserted at the specified location.
②, look slow , of course, if the subscript to find is very fast. But usually we look for the element value, given an element value, for an unordered array , we need to start with the first element of the array, until we find that element . An ordered array can be found by a particular algorithm faster than without an array, and we'll talk about sorting algorithms later on.
③, Delete slowly , according to the value of the element is deleted, we need to find the location of the element, and then the value behind the element to move the whole forward one position. It also takes a lot more time.
④, Arrays once created, the size is fixed, you cannot dynamically expand the number of elements of the array . If you give a large array size at the time of initialization, it will waste memory space, if given small, the number of subsequent data increase and add not go in .
Obviously, although the array is inserted fast, but the search and delete are slow , and the extensibility is poor , so we generally do not use the array to store the data, there is no structure to insert, find, Delete is fast, but also can dynamically expand the number of storage, the answer is yes, but this is based on a very complex algorithm, we will explain in detail later.
Structure and algorithm (1)-----array