Java implements a variable length array

Source: Internet
Author: User
Tags array length

1, array: An array is a data structure used to store the same type of collection, once the array is created, it cannot change its size.

2, since the length of the array is immutable, then how to achieve a variable length?

Idea: Since the array length is immutable, then we can create a new array with a different length from the original array, and let the original array variable point to the new array, thus allowing for a variable length (if the new array is not used, it will be recycled by the Java garbage collection mechanism, thus not wasting memory space.) )

A) Create a new array directly, and let the original array variable point to the new array.

Importjava.util.Arrays; /*** This is the class we use to test * *@author404NOTFound*/   Public classDemo { Public Static voidMain (string[] args) {int[] src =New int[0]; int[] Dest =New int[3]; SRC=dest; src[0] = 1; src[1] = 2; src[2] = 3;      System.out.println (arrays.tostring (SRC)); }  } 

Output Result:

b) Use the static method of the Arrays class or the System class: Arrays.copyof (), System.arraycopy (), the input length is different than the original array length of the parameter will be copied to create a different length of the array.

The code is as follows:

Import java.util.Arrays;    Public class Demo {      publicstaticvoid  main (string[] args) {          int  newint[0];           = arrays.copyof (src, 1);           -1] = 1;          System.out.println (arrays.tostring (SRC));      }  }  

c) Use ArrayList instead of arrays, with generic ArrayList to store different types of objects.

The code is as follows:

Importjava.util.ArrayList; Importjava.util.Arrays;  Public classDemo { Public Static voidMain (string[] args) {ArrayList<Integer> Al =NewArraylist<>(); Al.add (1); Al.add (2); System.out.println ("Output Length:" +al.size ()); Al.add (3); System.out.println ("Output Length:" +al.size ()); }  }  

Output Result:

d) Use Vector,vector to store homogeneous objects

The code is as follows:

 Packagetest; ImportJava.util.Vector;  Public classTest01 { Public Static voidMain (string[] args) {Vector vet=NewVector (); String Str= "Test1"; Doublet1=0.124;            Vet.add (str);           Vet.add (string.valueof (t1)); System.out.println ("Get Size:" +vet.size ()); System.out.println (Vet.get (0)); }        }    

Output Result:

Java implements a variable length array

Related Article

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.