1. Declaring an array
New String[5]; = {"A", "B", "C", "D", "E"}; New String[]{"A", "B", "C", "D", "E"};
2. Output an array
int [] Intarray = {1, 2, 3, 4, 5 }; = arrays.tostring (intarray); // Print directly would print reference value System.out.println (intarray); // [email protected ] System.out.println (intarraystring); // [1, 2, 3, 4, 5]
3. Creating an array list from an array
String[] Stringarray = {"A", "B", "C", "D", "E" }; ArrayListNew arraylist<string>(arrays.aslist (Stringarray)); System.out.println (arrayList); // [A, B, C, D, E]
4. Check if an array contains a value
String[] Stringarray = {"A", "B", "C", "D", "E" }; Boolean b = arrays.aslist (Stringarray). Contains ("a"); System.out.println (b); // true
5. Connect two arrays
int [] Intarray = {1, 2, 3, 4, 5 }; int [] IntArray2 = {6, 7, 8, 9, ten }; // Apache Commons Lang library int [] Combinedintarray = Arrayutils.addall (Intarray, intArray2);
6. Declares an inline array (array inline)
Method (new string[]{"A", "B", "C", "D", "E"});
7. Put the supplied array elements into a string
// containing the provided list of elements // Apache Common lang String j = stringutils.join (new string[] {"A", "B", "C"}, ","); System.out.println (j); // A, B, C
8. Converting an array list 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);
9. Converting an array to set (set)
New Hashset<string>(Arrays.aslist (Stringarray)); SYSTEM.OUT.PRINTLN (set); // [D, E, B, C, a]
10. Reverse an array
int [] Intarray = {1, 2, 3, 4, 5 }; Arrayutils.reverse (intarray); System.out.println (arrays.tostring (Intarray)); // [5, 4, 3, 2, 1]
11. Move the elements in the divisor group
int [] Intarray = {1, 2, 3, 4, 5 }; int [] removed = Arrayutils.removeelement (Intarray, 3); // Create a new array System.out.println (arrays.tostring (removed));
12. Converting integers to byte arrays
byte [] bytes = Bytebuffer.allocate (4). Putint (8). Array (); for (byte t:bytes) { System.out.format ("0x%x", T); }
12 best Practices for Java arrays