Ten common methods of arrays tool class
Original link: http://blog.csdn.net/renfufei/article/details/16829457
0. Declaring an array
New String[5]; = {"A", "B", "C", "D", "E"}; New String[]{"A", "B", "C", "D", "E"};
1. Print an array
int [] Intarray = {1, 2, 3, 4, 5 }; = arrays.tostring (intarray); // print directly, the hash value of the referenced object is printed // [email protected ] System.out.println (intarray); // [1, 2, 3, 4, 5] System.out.println (intarraystring);
2. Create ArrayList based on an array
String[] Stringarray = {"A", "B", "C", "D", "E" }; ArrayListNew arraylist<string>(arrays.aslist (Stringarray)); // [A, B, C, D, E] System.out.println (arrayList);
3. Check if the array contains a value
String[] Stringarray = {"A", "B", "C", "D", "E" }; Boolean b = arrays.aslist (Stringarray). Contains ("a"); // true System.out.println (b);
4. Merge connections two arrays
int [] Intarray = {1, 2, 3, 4, 5 }; int [] IntArray2 = {6, 7, 8, 9, ten }; // Apache Commons Lang gallery int [] Combinedintarray = Arrayutils.addall (Intarray, intArray2);
5. Declaring an inline array
Method (new string[]{"A", "B", "C", "D", "E"});
6. Concatenate (join) array with the given string
// containing the provided list of elements // Apache Common lang String j = stringutils.join (new string[] {"A", "B", "C"}, ","); // A, B, C System.out.println (j);
7. Convert ArrayList to Arrays
String[] Stringarray = {"A", "B", "C", "D", "E" }; ArrayListNew arraylist<string>(arrays.aslist (Stringarray)); New string[arraylist.size ()]; Arraylist.toarray (Stringarr); for (String S:stringarr) System.out.println (s);
8. Converting an array to a set
New Hashset<string>(Arrays.aslist (Stringarray)); // [D, E, B, C, a] SYSTEM.OUT.PRINTLN (set);
9. Array element inversion
int [] Intarray = {1, 2, 3, 4, 5 }; Arrayutils.reverse (intarray); // [5, 4, 3, 2, 1] System.out.println (arrays.tostring (Intarray));
10. Removing elements
int [] Intarray = {1, 2, 3, 4, 5 }; int [] removed = Arrayutils.removeelement (Intarray, 3); // Create a new array System.out.println (arrays.tostring (removed));
more--Convert int value to byte array
byte [] bytes = Bytebuffer.allocate (4). Putint (8). Array (); for (byte t:bytes) { System.out.format ("0x%x", T); }
Related reading:
- Example of sorting Arrays
- Read bytes from FileInputStream in Java
- Java:convert a file to a byte array, and then convert byte array to a file.
- Top Ten questions about Java collections
Java Foundation--arrays Tools Class Ten common methods