Home> Java> JavaBase> body text

10 commonly used methods for java arrays

Release: 2020-06-19 17:28:02
forward
4839 people have browsed it

10 commonly used methods for java arrays

Create an array:

String[] a = new String[5]; String[] b = {“a”,”b”,”c”, “d”, “e”}; String[] c = new String[]{“a”,”b”,”c”,”d”,”e”};
Copy after login

1. Print the array

We often use for loops or some iterators to print out all elements of the array. But we can also change the posture.

int array[] = {1,2,3,4,5}; System.out.println(array); //[I@1234be4e String arrStr = Arrays.toString(array); System.out.println(array); //[1,2,3,4,5];
Copy after login

2. Create ArrayList

String[] array = { “a”, “b”, “c”, “d”, “e” }; ArrayList arrayList = new ArrayList(Arrays.asList(array)); System.out.println(arrayList); // [a, b, c, d, e]
Copy after login

3. Check whether it contains a certain value

int array[] = {1,2,3,4,5}; boolean isContain= Arrays.asList(array).contains(5); System.out.println(isContain); // true
Copy after login

4. Connect two arrays

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

5. In one line Declare an array

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

6, invert the array

int[] intArray = { 1, 2, 3, 4, 5 }; // Apache Commons Lang library ArrayUtils.reverse(intArray); System.out.println(Arrays.toString(intArray)); //[5, 4, 3, 2, 1]
Copy after login

7, delete an element

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

8, convert it to set

Set set = new HashSet(Arrays.asList(new String[]{"a", "b", "c", "d", "e"})); System.out.println(set); //[d, e, b, c, a]
Copy after login

9, convert Array Convert List to Array

String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList arrayList = new ArrayList(Arrays.asList(stringArray)); String[] stringArr = new String[arrayList.size()]; arrayList.toArray(stringArr);
Copy after login

10. Compose array elements into a string

// Apache common lang String j = StringUtils.join(new String[] { "a", "b", "c" }, ", "); System.out.println(j); //a, b, c
Copy after login

For more related knowledge, please pay attention tojava basic tutorialcolumn

The above is the detailed content of 10 commonly used methods for java arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:weixin
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!