Top 10 methods for Java array manipulation

  • 2020-04-01 03:29:42
  • OfStack

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, which we call dynamic definition here.

The second and third also initialize values while allocating memory space.

Print the elements in a Java array


int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray);

// print directly will print reference value
System.out.println(intArray);
// [I@7150bd4d

System.out.println(intArrayString);
// [1, 2, 3, 4, 5]

The point here is to show the difference between an array reference in Java and an array reference in Java. The third line directly prints an intArray, and the output is garble, because an intArray is simply an address reference. Line 4 prints the actual array value because it has been converted to arrays.tostring (). For Java beginners, references and values are still important.

3. Create an 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 an Array to an ArrayList? Perhaps because ArrayList is a dynamic linked list, we can add or subtract ArrayList more easily. We do not need to loop Array to add every element to the ArrayList, and the above code can be used to simply implement the transformation.

4. Check if the array contains a 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 the Array to List< String> , so you can use the contains function of the dynamic list to determine whether an element is contained in the list.

5. Join two arrays


int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

ArrayUtils is an array-processing class library provided by Apache whose addAll method makes it easy to join two arrays into one.

Declare an array inner chain


method(new String[]{"a", "b", "c", "d", "e"});

7. Output the elements of the array as 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

Also using the join method in StringUtils, you can output the elements of an array as a string.

Convert Array to Set


Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);
//[d, e, b, c, a]

Using Set in Java makes it easy to save the required type as a collection type in a variable, mainly for displaying lists. You can also convert an Array to a List and then a List to a Set.

9. Flip the array


int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]

Still USES the versatile ArrayUtils.

Remove an element from the array


int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));

One more: converts an int value to a byte array


byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();

for (byte t : bytes) {
System.out.format("0x%x ", t);
}

English text: (link: http://java.dzone.com/articles/top-10-methods-java-arrays)
Translated by zhang nong. Com


Related articles: