Java Dynamic Array is an object that can be scaled at any time. arraylist is commonly used in Java, and arraylist is a Java. util. arraylist self-contained in Java API. The following describes how to use arraylist as a Java Dynamic Array.
1. Syntax: add () is to add a new element,
Remove () deletes an element,
Size () to obtain the length of the arraylist.
The subscript of arraylist starts from 0.
2. Sample Code
Import java. util. arraylist;
Public class javaarraylist {
Public static void main (string [] ARGs ){
// Java Dynamic Array Initialization
Arraylist Al = new arraylist ();
// Add data to the Java Dynamic Array
Al. Add ("");
Al. Add ("B ");
Al. Add ("C ");
// Output a Java Dynamic Array
For (INT I = 0; I <Al. Size (); I ++ ){
String aleach = (string) Al. Get (I );
System. Out. println (aleach );
}
// Delete an element in the array and delete the second element.
Al. Remove (1 );
// Modify the Java Dynamic Array and place the new element in the second position.
Al. Add (1, "2 ");
// Output a Java Dynamic Array
For (INT I = 0; I <Al. Size (); I ++ ){
String aleach = (string) Al. Get (I );
System. Out. println (aleach );
}
}
}
The output is as follows:
A
B
C
A
2
C
PS: Java's one-dimensional dynamic arrays generally use collections: arraylist, hashmap, etc.
Generic:
Arraylist <t> A = new arraylist <t> ();
T is the type stored in your array, such as the string type, such as the int type.
You can see the generic.