Home  >  Article  >  Java  >  Top 10 methods of java array operations

Top 10 methods of java array operations

伊谢尔伦
伊谢尔伦Original
2016-11-25 10:48:521662browse

The following are carefully compiled top 10 methods of Java array operations. Most of the codes come from Stack Overflow.

 0. 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.

 1. Print the elements in the 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 focus here is to explain the difference between the reference and value of the array 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.

 2. 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 do you need to 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.

 3. 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 the Array into Listf7e83be87db5cd2d9a8a0b8117b38cd4, so that you can use the contains function of the dynamic linked list to determine whether the element is included in the linked list.

 4. Connect 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 library provided by Apache. Its addAll method can easily connect two arrays into one array.

5. Declare an array internal link

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

6. Output the elements in 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 use the join method in StringUtils to output the elements in the array as a string.

 7. 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.

 8. Array flipping

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

  Still using the universal ArrayUtils.

 9、从数组中移除一个元素

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

 再补充一个:将一个int值转化成byte数组

byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for (byte t : bytes) {
System.out.format("0x%x ", t);
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn