Transformations between Java lists, collections, and arrays

Source: Internet
Author: User
Tags set set
Package test;

Import java.util.ArrayList;

Import Java.util.Arrays;

Import Java.util.HashSet;

Import java.util.List;

Import Java.util.Set;

		public class Test2 {public static void main (string[] args) {List List = new ArrayList ();

		List.add ("a");

		List.add ("B");

		List.add ("C");

		List.add ("D");

		List.add (1)//will produce java.lang.ArrayStoreException exceptions///When the data types in the list are consistent, the list can be converted to an array of object[] array = List.toarray ();

		System.out.println ("The length of the object array converted from list is:" + array.length); You need to force type conversion when converting to an array of other types, and to use the ToArray method with parameters, the argument is an object array,//the contents of the list are placed in the parameter array, and when the length of the argument array is less than the number of elements in the list,

		Automatically expands the length of the array to fit the length of the list string[] Array1 = (string[]) List.toarray (new string[0));

		System.out.println ("The length of the string array converted from List is:" + array1.length);

		Allocates a string array of length equal to the length of the list string[] Array2 = (string[]) List.toarray (new String[list.size ());

		System.out.println ("The length of the string array converted from List is:" + array2.length);

		List.clear (); Converts the array to list for (int i = 0; i < Array.Length i++) {List.add (array[i]);

		System.out.println ("The number of elements that convert the array to list is:" + list.size ());

		List.clear ();

		Direct use of Arrays aslist method list = arrays.aslist (array);

		System.out.println ("The number of elements that convert the array to list is:" + list.size ());

		Set set = new HashSet ();

		Set.add ("a");

		Set.add ("B");

		Converts set to array = Set.toarray ();

		Array1 = (string[]) Set.toarray (new string[0));

		Array2 = (string[]) Set.toarray (new String[set.size ()));

		System.out.println ("The length of the object array converted from set is:" + array.length);

		System.out.println ("The length of the string array converted from set is:" + array2.length);

		Array to set///To convert array to list, then use list to construct set set = new HashSet (arrays.aslist (array));

		System.out.println ("The number of elements that convert the array to set is:" + list.size ());

		Empty the set, and then convert the array to list all add set.clear ();

		Set.addall (Arrays.aslist (array1));

	System.out.println ("The number of elements that convert the array to set is:" + list.size ());
 }

}

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.