Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common examples of java array

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "java array which commonly used examples", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in-depth, together to study and learn "java array which commonly used examples" it!

The characteristics of the array:

Array is the simplest compound data type, it is a collection of ordered data, each element in the array has the same data type, you can use a unified array name and a different subscript to uniquely determine the elements in the array. According to the dimension of the array, it can be divided into one-dimensional array, two-dimensional array and multi-dimensional array.

In general, arrays have the following characteristics:

An array can be an one-dimensional, two-dimensional, or multidimensional array. The default value for numeric array elements is 0, while the default value for reference elements is null. An interlaced array is an array of arrays, so its elements are reference types and are initialized to null. The dimensions and sizes of staggered array elements can be different. The index of the array starts at 0, and if the array has n elements, the index of the array is from 0 to (nmur1). Array elements can be of any type, including array types. An array type is a reference type derived from the abstract base class Array.

There are three ways to define an array.

The first is to define an array and specify the length of the array, which we call a dynamic definition here. The second is to initialize the contents of the array directly, and the third initializes the value while allocating memory space.

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"}

Print the elements in the Java array

The reference to the array in Java is worth distinguishing. The third line prints intArray directly, and the output is garbled, because intArray is just an address reference. Line 4 outputs the real array value because it is converted by Arrays.toString (). For Java beginners, references and values still need to be paid attention to.

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]

Create an ArrayList from Array

Why convert Array to ArrayList? Maybe because ArrayList is a dynamic linked list, we can add and delete ArrayList more easily. We don't need to loop Array to add every element to ArrayList. We can simply implement the transformation with the following code.

String [] stringArray = {"a", "b", "c", "d", "e"}; ArrayList arrayList = new ArrayList (Arrays.asList (stringArray)); System.out.println (arrayList); / / [a, b, c, d, e]

Fourth, check whether the array contains a certain value

First use Arrays.asList () to convert Array to List, so that you can use the contains function of the dynamic linked list to determine whether the element is contained in the linked list.

String [] stringArray = {"a", "b", "c", "d", "e"}; boolean b = Arrays.asList (stringArray) .duration ("a"); System.out.println (b); / / true

Connect two arrays

ArrayUtils is an array processing class library provided by Apache, and its addAll method can easily join two arrays into an array.

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

Output the elements in the array as a string

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

/ / containing the provided list of elements// Apache common langString j = StringUtils.join (new String [] {"a", "b", "c"}, ","); System.out.println (j); / / a, b, c

7. Convert Array to Set collection

Using Set in Java, you can easily save the desired type as a collection type in a variable, mainly for display lists. You can also convert Array to List and then List to Set.

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

8. Array flipping

Use the. Reverse () method array in Apache's ArrayUtils utility class to reverse the method. You can also specify the reverse position of the start and end.

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

Remove an element from the array

Use the removeElement () method in Apache's ArrayUtils utility class to remove the first occurrence of the specified element from the array and return a new array

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

Thank you for your reading, the above is the content of "what are the common examples of the java array". After the study of this article, I believe you have a deeper understanding of the problem of what common examples of the java array are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report