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 and size () gets the length of arraylist. The subscript of arraylist starts from 0.
2. Sample Code
Package wang48.jiaocheng;
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 dynamic Java 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