0. Create/Declare an array
| 123 |
String[] aArray = new String[5];String[] bArray = {"a","b","c", "d", "e"};String[] cArray = new String[]{"a","b","c","d","e"}; |
Print an array in 1.Java
| 123456789 |
int[] intArray = { 1, 2, 3, 4, 5};String intArrayString = Arrays.toString(intArray); // print directly will print reference valueSystem.out.println(intArray);// [[email protected]System.out.println(intArrayString);// [1, 2, 3, 4, 5] |
2. Create a ArrayList with an array
| 1234 |
String [ ] stringArray = { "a", "b" , "c" , "d" , "e" } ; ArrayList < String > arrayList = newArrayList < String > ( Arrays . asList ( stringArray ) ) ; System . out . println ( arrayList ) ; // [A,B,C,D,E] |
3, check whether the array contains a specific value
| 123 |
String[] stringArray = { "a", "b", "c", "d", "e"};booleanb = Arrays.asList(stringArray).contains("a");System.out.println(b); |
4. Combining two arrays
| 1234 |
int[] intArray = { 1, 2, 3, 4, 5 };int[] intArray2 = { 6, 7, 8, 9, 10 };// Apache Commons Lang libraryint[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2); |
5. Methods for declaring an array
| 1 |
method(newString[]{"a", "b", "c", "d", "e"}); |
6, joins the elements in the supplied array into a string
| 12345 |
// containing the provided list of elements// Apache common langString j = StringUtils.join(newString[] { "a", "b", "c"}, ", ");System.out.println(j);// a, b, c |
7. Conversion between array and list
| 123456 |
string[] Stringarray = { Code class= "Java plain", "C" "D" "E" arraylist<string> ArrayList = new arraylist<string> (arrays.aslist (Stringarray)); string[] Stringarr = new string[arraylist.size ()]; arraylist.toarray (Stringarr); for system.out.println (s); |
8. Array conversion to set
| 123 |
Set<String> set = newHashSet<String>(Arrays.asList(stringArray));System.out.println(set);//[d, e, b, c, a] |
9. Array reverse output
| 1234 |
int[] intArray = { 1, 2, 3, 4, 5};ArrayUtils.reverse(intArray);System.out.println(Arrays.toString(intArray));//[5, 4, 3, 2, 1] |
10. Deleting an array element
| 123 |
int[] intArray = { 1, 2, 3, 4, 5};int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new arraySystem.out.println(Arrays.toString(removed)); |
Finally, int is converted into a byte array
| 12345 |
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();for (bytet : bytes) {System.out.format("0x%x ", t);} |
Common methods for arrays in Java