java-Basics-generic array list-resolves an issue in which arrays are dynamically changed at run time

Source: Internet
Author: User

Generic array list, mainly to solve the problem of dynamically changing arrays at runtime

Usually we define an array of people in a department,

But in real business, the size of this array is often indeterminate,

If the definition is too large, then the space is wasted, the definition is too small, and not enough,

In order to solve the problem of dynamically changing arrays at runtime, we propose the following solution.


Package com.ray.object;import java.util.arraylist;/** * Generic array list, resolving the problem of dynamically changing arrays at runtime *  * @author Ray * @since 2015-05-04 * @ Version 1.0 *  */public class Person {private int id = 0;public int getId () {return ID;} public void setId (int id) {this.id = ID;} public static void Main (string[] args) {//Normally we will define the people in a department as in the following statement//But in the actual business, the size of this array is often uncertain//if the definition is too large, then waste space, defined too small, Not enough//because in order to solve the problem of dynamically changing arrays at runtime, we propose the following solution//person[] persons = new person[100];//assemble a person's listarraylist<person> List = new arraylist<person> (); for (int i = 0, i < 5; i++) {Person temp = new person (); Temp.setid (i); List.add (tem p);} person[] persons = new Person[list.size ()];//through the method, the list is converted to the corresponding array list.toarray (persons); for (int i = 0; i < Persons.lengt H i++) {System.out.println (Persons[i].getid ());}}}


java-Basics-generic array list-resolves an issue in which arrays are dynamically changed at run time

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.