ARRAYS CLASS
A class that contains some static methods that are used with arrays
Such as ::
1. Sorting
2.Searching
3.Comparing
4.Returning a string representation of an array
SORTING ARRAY(using sort() method)
Syntax :-> // sort(array, fromIndex, toIndex): sort from (fromIndex) to (toIndex 1)
//sort(array): sorts the whole array
int[] numbers = {5, 2, 3, -1, 0, 4, 1};
Arrays.sort(numbers); // -1, 0, 1, 2, 3, 4, 5
char[] characters = {'a', 'z', 'b', 'w', 'c', 'A', 'D', 'Z', 'C'}; Arrays.sort(characters); // A, C, D, Z, a, b, c, w, z
int[] unicodes = {'a', 'z', 'b', 'w', 'c', 'A', 'D', 'Z', 'C'}; Arrays.sort(unicodes); // 65, 67, 68, 90, 97, 98, 99, 119, 122
int[] numbers = {5, 4, 3, 2, 1, 0, -1}; // 3, 6 + 1 Arrays.sort(numbers, 3, 7); // 5, 4, 3, -1, 0, 1, 2
SEARCHING ARRAY(using Using binarySearch() method)
The array should be sorted in increasing order
Syntax:->binarySearch(array, element)
Example→ binarySearch(numbers, 4)
=> Return values:
- Index of element inside the array if exists
- (insertionIndex + 1) if the element was not found
- Example -> {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}
int[] numbers = {5, 4, 3, 2, 1, 0, -1}; Arrays.sort(numbers); // -1, 0, 1, 2, 3, 4, 5
System.out.println( Arrays.binarySearch(numbers,4) ); // 5
System.out.println( Arrays.binarySearch(numbers,3) ); // 4
System.out.println( Arrays.binarySearch(numbers,-3) ); // -1
System.out.println( Arrays.binarySearch(numbers,6) ); // -8
String[] strings = {"a", "b", "c"};
System.out.println(Arrays.binarySearch(strings,"a")); // 0
System.out.println(Arrays.binarySearch(strings,"c")); // 2
System.out.println(Arrays.binarySearch(strings,"A")); // -1
System.out.println(Arrays.binarySearch(strings,"d")); // -4
// fill(array, value): fill whole array
int[] numbers1 = new int[4]; // { 0,0,0,0}
Arrays.fill(numbers1, 3); // {3,3,3,3}
// fill(array, fromIndex, toIndex, value)
int[] numbers2 = new int[8]; // {0, 0, 0,0,0,0,0,0}
Arrays.fill(numbers2, 3, 7, 5); //{0,0,0,5,5,5,5,0}
A class that contains some static methods that are used with arrays
Such as ::
1. Sorting
2.Searching
3.Comparing
4.Returning a string representation of an array
SORTING ARRAY(using sort() method)
Syntax :-> // sort(array, fromIndex, toIndex): sort from (fromIndex) to (toIndex 1)
//sort(array): sorts the whole array
int[] numbers = {5, 2, 3, -1, 0, 4, 1};
Arrays.sort(numbers); // -1, 0, 1, 2, 3, 4, 5
char[] characters = {'a', 'z', 'b', 'w', 'c', 'A', 'D', 'Z', 'C'}; Arrays.sort(characters); // A, C, D, Z, a, b, c, w, z
int[] unicodes = {'a', 'z', 'b', 'w', 'c', 'A', 'D', 'Z', 'C'}; Arrays.sort(unicodes); // 65, 67, 68, 90, 97, 98, 99, 119, 122
int[] numbers = {5, 4, 3, 2, 1, 0, -1}; // 3, 6 + 1 Arrays.sort(numbers, 3, 7); // 5, 4, 3, -1, 0, 1, 2
SEARCHING ARRAY(using Using binarySearch() method)
The array should be sorted in increasing order
Syntax:->binarySearch(array, element)
Example→ binarySearch(numbers, 4)
=> Return values:
- Index of element inside the array if exists
- (insertionIndex + 1) if the element was not found
- Example -> {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}
int[] numbers = {5, 4, 3, 2, 1, 0, -1}; Arrays.sort(numbers); // -1, 0, 1, 2, 3, 4, 5
System.out.println( Arrays.binarySearch(numbers,4) ); // 5
System.out.println( Arrays.binarySearch(numbers,3) ); // 4
System.out.println( Arrays.binarySearch(numbers,-3) ); // -1
System.out.println( Arrays.binarySearch(numbers,6) ); // -8
String[] strings = {"a", "b", "c"};
System.out.println(Arrays.binarySearch(strings,"a")); // 0
System.out.println(Arrays.binarySearch(strings,"c")); // 2
System.out.println(Arrays.binarySearch(strings,"A")); // -1
System.out.println(Arrays.binarySearch(strings,"d")); // -4
// fill(array, value): fill whole array
int[] numbers1 = new int[4]; // { 0,0,0,0}
Arrays.fill(numbers1, 3); // {3,3,3,3}
// fill(array, fromIndex, toIndex, value)
int[] numbers2 = new int[8]; // {0, 0, 0,0,0,0,0,0}
Arrays.fill(numbers2, 3, 7, 5); //{0,0,0,5,5,5,5,0}