1. Define a Java array
String[] aArray = new String[5];String[] bArray = {"a","b","c", "d", "e"};String[] cArray = new String[]{"a ","b","c","d","e"};
The first is to define an array and specify the length of the array. We call it dynamic definition here.
The second and third types allocate memory space and also initialize the value.
2. Print elements in Java array
int[] intArray = { 1, 2, 3, 4, 5 };String intArrayString = Arrays.toString(intArray);// print directly will print reference valueSystem.out.println(intArray);// [I@7150bd4dSystem. out.println(intArrayString);//[1, 2, 3, 4, 5]
The key point here is to illustrate the difference between the reference and value of arrays in Java. The third line directly prints intArray, and the output is garbled code, because intArray is just an address reference. Line 4 outputs the real array value because it has been converted by Arrays.toString(). For Java beginners, references and values still need to be paid attention to.
3. Create ArrayList from Array
String[] stringArray = { "a", "b", "c", "d", "e" };ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));System.out .println(arrayList);//[a, b, c, d, e]
Why convert Array to ArrayList? Perhaps because ArrayList is a dynamic linked list, we can add, delete, and modify ArrayList more conveniently. We do not need to loop through Array to add each element to ArrayList. The conversion can be easily achieved with the above code.
4. Check whether the array contains a certain value
String[] stringArray = { "a", "b", "c", "d", "e" };boolean b = Arrays.asList(stringArray).contains("a");System.out.println( b);// true
First use Arrays.asList() to convert Array into List<String>, so that you can use the contains function of the dynamic linked list to determine whether the element is included in the linked list.
5. Connect two arrays
int[] intArray = { 1, 2, 3, 4, 5 }; int[] intArray2 = { 6, 7, 8, 9, 10 }; // Apache Commons Lang libraryint[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
ArrayUtils is an array processing class library provided by Apache. Its addAll method can easily connect two arrays into one array.
6. Declare an array internal link
method(new String[]{"a", "b", "c", "d", "e"});
7. Output the elements in the array as strings
// containing the provided list of elements// Apache common langString j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");System.out.println(j) ;// a, b, c
Also using the join method in StringUtils, the elements in the array can be output in the form of a string.
8. Convert Array into Set collection
Set<String> set = new HashSet<String>(Arrays.asList(stringArray));System.out.println(set);//[d, e, b, c, a]
Using Set in Java, you can easily save the required type in a variable as a collection type, mainly used in display lists. You can also convert Array to List first, and then convert List to Set.
9. Array flipping
int[] intArray = { 1, 2, 3, 4, 5 };ArrayUtils.reverse(intArray);System.out.println(Arrays.toString(intArray));//[5, 4, 3, 2, 1 ]
The universal ArrayUtils is still used.
10. Remove an element from an array
int[] intArray = { 1, 2, 3, 4, 5 };int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new arraySystem.out.println(Arrays.toString(removed));
One more thing: convert an int value into a byte array
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();for (byte t : bytes) {System.out.format("0x%x ", t);}
Original English text: Top 10 Methods for Java Arrays
Translation author: Xiaofeng from MaNongwang