Array of tasteless food and pitiful discard in Java

Before the emergence of Java generics, only arrays could be used to store objects of specified types; Before the emergence of automatic boxing mechanism, only arrays could be used to store basic data types; In other words, before the emergence of generics and automatic boxing mechanism, arrays played an important role in Java.

Moreover, array is also the most efficient way to store and randomly access object sequences, but unfortunately, the length of array is fixed - when it is created, the specified length is 6, so it can only store 6 elements. When you want to put the seventh element, you can't do it.

With the improvement of computer hardware capability, developers prefer to use ArrayList rather than array when storing a set of data. Although ArrayList is implemented internally through arrays, its capacity can grow automatically compared with arrays, and there are many features that arrays do not have.

To be sure, don't use arrays unless you want to use arrays to improve performance.

So why learn arrays? Because the designers of Java didn't kill arrays, just like Cao Cao didn't throw away the tasteless chicken ribs.

Let's give a clear definition of Java arrays -- arrays are used to store fixed length elements of the same type. Examples are as follows: int [] ints is the preferred declaration method compared with int ints1 []. The style of int ints1 [] comes from C / C + + language, so that C / C + + programmers can quickly understand java language. There are two ways to create an array. One is to specify the length through the new keyword, and then assign the value through array [index] = value; Another way is through {value1, Value2,...} Directly create and assign values at the same time. The most common applications are as follows (placeholder strings for dates):

When using list and map, You can directly obtain the actual size (length) of the container through the size () method. The array also has a keyword length to obtain the size (length), but the meaning is very different. Length only represents the size of the elements that the array can hold, not the actual size of the array. For example, although the actual length of ints is 2, the length of ints.length is 3 - be careful.

The designer of Java has worked hard to design a special tool class for arrays - Java util. Arrays, there are several common methods. Let's introduce them. 1) Sort () the sort () method is used to sort the array. This method requires that the elements of the array implement the comparable interface. If the sorted element is not a string or basic data type, you need to actively implement the comparable interface (the basic data type and string themselves have implemented the comparable interface). An example is as follows: the result of sorting by the sort() method is [2, Shen, Wang, Mo]. 2) Binarysearch() the binarysearch() method is used to binary search the array (return the subscript of the value, and - 1 if it is not found). Before calling this method, you must sort it first. An example is as follows: because the sorted result of the sort() method is [2, default], so arrays Binarysearch (STRs, "two") returns the subscript value 0. 3) The aslist() aslist() method is used to convert an array into a list (ArrayList). The source code is as follows: (a);} How do you convert a list into an array? Through the toArray () method of list. You can refer to the following example to experience the subtle differences caused by different array definition methods.  list = Arrays. asList(strs); System. out. println(list); When the declared array size exceeds the size of the list, the toarray() method will make up (null) the converted array. 4) tostring() tostring() method is used to output the array in string format ([value1, value3,...] The source code is as follows: Recommended Reading:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>