Java Collection (Tool class arrays)

Source: Internet
Author: User

/*
* Arrays: A tool class for manipulating arrays, which are static methods.


* ToString Method: Returns a string representation of the contents of the specified array.


* Aslist method: Returns a fixed-size list supported by the specified array


* ToArray method: Turns the collection into an array.

*/
Import java.applet.*;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.List;
public class CollectionsDemo3 {
public static void Main (string[] args) {
/*int arr[]={2,4,5};
SOP (Arrays.tostring (arr));
String[] arr1={"abc", "CC", "KKK"};
/*
* Sets the array into a collection and can manipulate the array using the method of the collection.
* However, you cannot use the collection's additions and deletions because the length of the array is different.
* An unsupported operation exception will occur.

List<string> list=arrays.aslist (ARR1);
SOP (List.contains ("CC"));
List.add ("Rew");
SOP (list); */
/*
* If the elements in the array are objects, then all the elements become the elements of the collection.
* If the elements in the array are basic data, then the array becomes an element after it becomes a collection.
*
Integer [] nums={2,4,5};
List li=arrays.aslist (nums);
SOP (LI); */

/*
* collection becomes an array.
* 1. When the array length of the specified type is less than the size of the collection, a new array is created inside the method, with a length of size.
* 2. When an array of the specified type is larger than the size of the music collection, the new array is not created, but the passed in array is used, and the extra number is supplemented with null.
* So create an equal size array optimal.
* Why do you want to change the collection into an array?
* To limit the operation of elements, no additions or deletions are required.
*/


Arraylist<string> a1=new arraylist<string> ();
A1.add ("ABC1");
A1.add ("ABC2");
A1.add ("ABC3");
String[] Arr=a1.toarray (new string[1]);
SOP (Arr[1]);
SOP (Arrays.tostring (arr));
}
public static <T> void sop (T t)
{
System.out.println (t);
}
}



Import java.util.*;


/*
* for (String s:a1)
SOP (s);

The new output method of version 1.5, instead of the original iterator, outputs the elements in the list collection, but cannot make any modification actions.
There must be a traversal target.
*/


public class CollectionsDemo4 {
public static void Main (string[] args) {
Arraylist<string> a1=new arraylist<string> ();
A1.add ("ABC1");
A1.add ("ABC2");
A1.add ("ABC3");
for (String s:a1)
SOP (s);
Hashmap<integer,string> hm=new hashmap<integer,string> ();
Hm.put (1, "a");
Hm.put (2, "B");
Hm.put (3, "C");
For (Integer I:hm.keyset ())
SOP (i+ "::" +hm.get (i));
For (map.entry<integer,string> Me:hm.entrySet ())
SOP (me+ "::" + "--" +me.getkey () +me.getvalue ());

}
public static <T> void sop (T t)
{
System.out.println (t);
}
}



/*
* New features in version 1.5, 1. Variable parameters:
* is a shorthand form of an array parameter that does not have to be set up manually every time the array object is created.
* Simply pass these elements of the operation as parameters.
* These parameters are implicitly encapsulated as an array.
* NOTE: Variable parameters must be defined at the end of the parameter list.


* 2.StaticImport static import.
* When the class name is the same, you need to specify a specific package name.
* Specifies the owning object or class when the name of the method is the same.
*/


Import java.util.*;//Importing all the classes in the Util
Import static java.lang.system.*;
Import static java.util.arrays.*;//Imports Arrays all static members in this class.
public class CollectionsDemo5 {
public static void Main (string[] args) {
Show (2,3,45,5);
Int[] arr={3,1,5};
After a static import, you can omit the writing of the class name and simplify the code
Sort (arr);
//
SOP (Arrays.tostring (arr));
int Index=binarysearch (arr, 1);
SOP (index);
}
public static void Show (int ... arr)
{
SOP (ARR);
}
public static <T> void sop (T t)
{
Out.println (t);
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Collection (Tool class arrays)

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.