It will be much convenient for us to use Arrays methods to deal with some problems, and here I will summarize some Arrays (java.util.Arrays) knowledge.
1. The java.util.Arrays class offers the following method for representing int arrays as strings
(1) Method: toString(int[] a)
input:
int [] a = {1,3,5};
System.out.println( Arrays.toString(a) );
output: [[I@1c78e57, [I@5224ee]
(2) Method: deepToString(int[] a)
input:
int [][] a = {
{1,2},
{3,4}
};
System.out.println( Arrays.deepToString(a) );
output: [[1, 2], [3, 4]]
2. The java.util.Arrays class offers the following method for sorting int arrays in non-decreasing order.
(1) Method: sort(int[] a)
input:
int [] a = { 3, 1, 5 };
Arrays.sort( a );
System.out.println( Arrays.toString(a) );
output:[1, 3, 5]
(2) Method: binarySearch(int[] a, int key)
input:
int [] a = { 1, 2, 3, 4, 5 };
System.out.print( Arrays.binarySearch( a, 2 ) );
System.out.println( Arrays.binarySearch( a, 6 ) );
output: 1, -6
No comments:
Post a Comment