Java Dynamic Array

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.