Import Java. lang. reflect. array;/*** Generic Dynamic Array class **/public class dynamicarray <t extends Object> {private T [] arr; private class <t> type; private int currcount; private Static final int initsize = 2; Public dynamicarray (class <t> type) {This. type = type; arr = createarray (initsize);}/*** in Java, you cannot directly create an array using t [] Tarr = new T [10, the simplest way is to use array. newinstance (class <t> type, int size) to create an array * For details, see http: // Dev Eloper.51cto.com/art/201202/317813.htm * @ Param size * @ return */@ suppresswarnings ("unchecked") Private T [] createarray (INT size) {return (T []) array. newinstance (type, size);} public void add (T number) {If (currcount> = arr. length) {// the actual number has exceeded the length of the array. You need to resize T [] arrnew = createarray (ARR. length * 2); // resize // fill in the original value for (Int J = 0; j <arr. length; j ++) {arrnew [J] = arr [J];} // discard the original array space. Arr points to the newly created array space (created in the heap) arr = arrnew; Arr [currcount] = number; currcount ++;} else {// arr [currcount] = number; currcount ++;} public void display () {system. out. print ("valid dynamic array element:"); For (INT I = 0; I <currcount; I ++) {system. out. print (ARR [I] + ",");} system. out. println ();} public void displayall () {system. out. print ("all elements in the dynamic array are:"); For (INT I = 0; I <arr. length; I ++) {system. out. print (ARR [I] + ",");} system. out. println ();} public T [] getarray (){ T [] arroutput = createarray (currcount); For (INT I = 0; I <currcount; I ++) {arroutput [I] = arr [I];} return arroutput;} public t get (INT index) {return arr [Index];} public static void main (string [] ARGs) {dynamicarray <integer> d = new dynamicarray <integer> (integer. class); D. add (1); D. add (2); D. add (3); D. add (4); D. add (5); D. add (6); D. add (7); D. add (8); D. add (9); D. display (); D. displayall (); integer [] arroutp Ut = D. getarray (); system. out. print ("arroutput:"); For (INT I = 0; I <arroutput. length; I ++) {system. out. print (arroutput [I] + ",");} system. out. println (); system. out. println ("element 8th is" + D. get (8); dynamicarray <string> D2 = new dynamicarray <string> (string. class); d2.add ("silly red dust"); d2.add ("boring"); d2.add ("empty eyes"); d2.add ("not in this life "); d2.add ("No worries"); d2.add ("just want to get a better chance"); d2.add ("laugh at people when you wake up"); d2.add ("Forget all dreams "); d2.add ("Sigh dark Too early "); d2.add (" difficult to generate "); d2.add (" Love and hate "); d2.add (" I only wish I could be happy "); d2.add ("Wind and cold do not want to escape"); d2.add ("Flowers and beautiful do not want"); d2.add ("let me shake "); d2.add ("the higher the day, the smaller the Heart"); d2.add ("do not ask how many causal factors exist"); d2.add ("drunk alone"); d2.add ("cry today and laugh tomorrow "); d2.add ("Don't ask anyone to understand"); d2.add ("one proud"); d2.add ("Singing and Dancing"); d2.add ("Long night without knowing "); d2.add ("search for happiness"); d2.add ("PS: smile is the embodiment of the true meaning of life and the attitude to cope with life. Learn to smile freely, and there will be no ups and downs in your life. "); D2.display (); d2.displayall ();}}
Output:
Valid dynamic array elements: 1, 2, 3, 4, 5, 6, 7, 8, 9. All dynamic array elements are: 1, 2, 3, 4, 6, 7, 8, 9, null, null, null, null, arroutput: 1, 2, 3, 4, 5, 6, 7, 8, 9, and 8th. The valid elements of the dynamic array are: red dust, silly, boring, and empty, this life has not been, but the heart is no longer disturbing, just want to change for a half-time happy, wake up to laugh at people, all forget in the dream, sigh dark too early, the next life is unpredictable, love and hate a pen off, I only wish I could be happy to be old, I don't want to escape from the cold wind, I don't want to be beautiful, I want to shake, the higher the sky, the smaller the heart, no matter how much cause and effect there is, drunk alone, today, I cried and laughed tomorrow. I don't want anyone to understand it. I'm proud that the song is singing and dancing. I don't know how long the night is, and I will look for happiness. PS: smiling is the embodiment of the true meaning of life, it is also an attitude to cope with life. Learn to smile freely, so there will be no ups and downs in your life ., All the elements of the dynamic array are: red dust, silly, boring, and empty-eyed. If this life is over, the heart is no longer disturbing. I just want to change to a half-time, laugh at people when I wake up, and forget all my dreams, sigh it's dark too early, it's hard to get a life, love and hate. I just want to be happy to get old when the wind is cold and don't want to escape, and I don't want to spend any more beautiful flowers. Let me shake it, the higher the sky, the smaller the heart, don't ask how much cause and effect there are, alone drunk, today cry tomorrow laugh, don't ask someone to understand, a proud, singing and dancing in the dance, long night don't know, will be happy to find, PS: smile is the embodiment of the true meaning of life and the attitude to cope with life. Learn to smile freely, so there will be no ups and downs in your life ., Null, null,