Method of "Java" in arrays to int array, integer array and int array

Source: Internet
Author: User

In java.util.*, arrays seems to be useful, and it seems that using the in-loop method avoids the need to write the lookup, initialization, and sequencing of the array.

First, to talk about the arrays in the shape of the array really useful method

1, the first is arrays.tostring ()

In general, print an array directly with SYSTEM.OUT.PRINTLN (), such as this:

int array[] = {8, 7, 100, 88, 6, 4, 5, 33, 10}; SYSTEM.OUT.PRINTLN (array);
Out of the result is a bunch of garbled:

[ljava.lang.integer;@4f19c297

Shaped arrays do not print directly like arraylist<integer>,

But if it is written like this:

int array[] = {8, 7, 100, 88, 6, 4, 5, 33, 10}; System.out.println (arrays.tostring (array));
It really can be printed as:

[8, 7, 100, 88, 6, 4, 5, 33, 10]

Therefore, using arrays.tostring () can effectively avoid writing a for loop

2, Arrays.fill (); Causes a value to be populated with an array. For example:

int array_init[]=new int[20]; Arrays.fill (array_init, 0); System.out.println (arrays.tostring (array_init));
Print out is 20 0, that is, the value of array_init at this time, this method in the array initialization is slightly useful, you can avoid a for loop ...

3, arrays.copyof (); it provides a copy of an array of elements into another array, which can also take some parameters, specify the scope of replication, you can avoid the use of the For loop to do this operation, this is meaningless, the actual operation is seldom used.

Second, however, the arrays is also a lot of plastic arrays do not use the method

1, although the inside of the Arrays.binarysearch (); Provides a lookup function, but in fact this method does not return the current position of the element you are looking for, it is to return the element you are looking for in the array in ascending order after the position, and can not find the return-1, But do not know why the following procedure:

int array[] = {8, 7, 100, 88, 6, 4, 5, 33, 10}; System.out.println (arrays.tostring (array)); System.out.println (Arrays.binarysearch (array,7)); Arrays.sort (array); System.out.println (arrays.tostring (array)); System.out.println (Arrays.binarysearch (array,7));
Output A-81 3, obviously 7 in the order before the order is in this array Ah!
2, Arrays.sort (); provides the array ascending sort function

But there is no descending sort function, someone tried to use Arrays.sort (array, Collections.reverseorder ()), to solve this problem, but this code for int array[] = {8, 7, 100, 88, 6, 4, 5, 33, 10}; is not compiled, for integer array[] = {8, 7, 100, 88, 6, 4, 5, 33, 10}; OK, for example:

Integer array[] = {8, 7, 100, 88, 6, 4, 5, 33, 10}; System.out.println (arrays.tostring (array)); Arrays.sort (Array, Collections.reverseorder ()); System.out.println (arrays.tostring (array));
The output:

[8, 7, 100, 88, 6, 4, 5, 33, 10]
[100, 88, 33, 10, 8, 7, 6, 5, 4]

Obviously achieves the effect we want, but it doesn't work if you change the array from integer to int. Although JDK1.5 after what the array of integers and int is automatically recognized by the system, and then what method is not provided, the web also said that the conversion has no practical significance, but in fact, in such a statement compiled does not pass, for the descending order of int you still have to write for the for loop honestly, or use my last "Java" Java Collections class--java in the data structure of the upgraded version (click the Open link)

3, as to the ArrayList and int array of the conversion of the other, or to honestly use for loop, only the integer array has a specific method, ArrayList in the ToArray () and Arrays aslist () do not know the meaning of it? Seemingly in a strongly pushed integer array instead of an int array? After all, the methods in the integer array are more. But textbooks are still a bunch of arrays of int teaching.

Method of "Java" in arrays to int array, integer array and int array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.