In Java, as one of the fastest stores and fetches in all data structures, arrays can be powerful in every way with this easy-to-use advantage. But arrays also have limitations of their own. The length of the array must be fixed once it is defined and cannot be changed dynamically, which creates the problem that if the array is full, you cannot continue adding data (you can, of course, define a "large enough array", but the question is how big is big enough?). Too small enough, too big to waste memory space). If you delete a data, its memory space is not used. In addition, the array can only store the same type of data, if it is set to object type, it is possible to save different types of data, but imagine a situation: Now the project has a definition of an object type of an array. The project has a lot of graphics classes, it is true that the array can be saved, but the problem is that when the user saves a different type of things, the program will not error, because the type is object. This is certainly not going to work. In addition, it is safe to move up in Java, the object type can accommodate all types, but it is not safe to move down. For example, the following code:
1 PackageCbs;2 3 Public classTest {4 5 Public Static voidMain (string[] args) {6 //defines an array of type Object7Object[] Array=NewObject[10];8 //wrapper classes that use basic data types9Integer i=10;TenFloat f=12.0f; One //change up, no problem Aarray[0]=i; -array[1]=F; - //Down Transformation the intI1= (int) array[0]; - //Note that the 12.0 is stored in array[1], but there is no problem with the syntax, and compilation is not an error . - intI2= (int) array[1]; - System.out.println (i1); +System.out.println (I2);//Throw ClassCastException Exception - } + A}
View Codein other words, if you use such an array, it is difficult to find problems in the transformation. This is also a limitation of the array. so since the array has such flaws, we have to find a way to solve this problem. How to solve it? The length of the array is of course not changeable, but the array name stores the array's first address in memory, which can be changed. Is it possible to dynamically change the size of an array by changing the address? The answer is yes. We can declare a new array, increase its size to the extent we want, then copy the value of the original array into the new array and assign the new array to the original array, so that the size of the array changes dynamically. At this point, add one more data to the new array length, reduce one data, let the new array length minus one. The problem with the length of the array is solved. So how should the data type be solved? This is going to be done with Java generics. Generic Java is not a data type, but a symbol to represent the type, the commonly used generic notation symbols are E, K, Y, and so on. This allows us to specify generics when defining a class, and then use the specified type return and input in the class, so that there is no problem with the type. The next thing to do is define a class to manipulate the array, which is the array queue we want to talk about.
1 PackageCom.cbs;2 3 /**4 * Custom Array queue5 * @authorCBS6 */7 8 Public classmylist<e> {//using generic e9 //declares an object arrayTen Privateobject[] array; One Private intsize=0;//record the length of the data type in the queue A Private intinitcount=0;//the size of the array at initialization, which defaults to 0 - Private intIncrescount=1;//when the array is full, the length of each array grows - /** the * Construction Method - * @paramInitcount The initial length of an array - */ - PublicMyList (intInitcount) { + This. initcount=Initcount; -array=NewObject[initcount]; + } A /** at * Construction Method - * @paramInitcount The initial length of an array - * @paramIncrescount is an array full of growth lengths each time - */ - PublicMyList (intInitcount,intIncrescount) { - This. Initcount =Initcount; in This. Increscount =Increscount; -array=NewObject[initcount]; to } + - //implementation of data additions the Public voidAdd (e e) { * if(size<Initcount) { $array[size]=e;Panax Notoginseng } - Else{ theObject[] Newarray=Newobject[array.length+Increscount]; +newarray[size]=e; A for(inti=0;i<array.length;i++){ thenewarray[i]=Array[i]; + } -initcount+=Increscount; $array=NewArray; $ } -size++; - } the //implements deleting data that specifies the location of the subscript - Public voidDeleteinti) {Wuyisize--; theinitcount--; -Object[] Newarray=NewObject[array.length-1]; Wu for(intj=0;j<i;j++) -newarray[j]=Array[j]; About for(intj=i+1;j<array.length;j++) $newarray[j-1]=Array[j]; -array=NewArray; - } - //implement inserting data into the specified subscript location A Public voidInsert (e E,inti) { + if(Size<initcount && i>=0 && i<size-1){ the for(intj=size;j>i;j--) -Array[j]=array[j-1]; $array[i]=e; the } the Else{ theObject[] Newarray=Newobject[array.length+Increscount]; the for(intj=0;j<array.length;j++){ -newarray[j]=Array[j]; in } theinitcount+=Increscount; thearray=NewArray; About for(intj=size;j>i;j--) theArray[j]=array[j-1]; thearray[i]=e; the } +size++; - } the //implements the data that gets the specified subscript locationBayi PublicE Get (inti) { the if(i<0 | | i>=size) the return NULL; - Else - return(E) array[i]; the } the //implementing data that updates the specified subscript location the Public voidUpDate (e E,inti) { the if(I>=0 && i<size) -array[i]=e; the } the //gets the number of elements stored in the array queue the Public intLegnth () {94 returnsize; the } the the}
View Code
Such a custom array pair of columns is done. Of course, the custom array is optimized for the column, which allows the user to specify the initial queue length and the length of time each team grows. This does not create a new array each time, compared to the above-mentioned length plus one. In this way, it is convenient to manipulate the array queue as long as the MyList object is instantiated in the class.
Custom array queues in Java