Klasa Arrays w Javie
The Tablice klasa w pakiet java.util jest częścią Struktura kolekcji Java . Ta klasa udostępnia metody statyczne umożliwiające dynamiczne tworzenie i uzyskiwanie dostępu Tablice Java . Zawiera wyłącznie metody statyczne oraz metody klasy Object. Metody tej klasy mogą być używane przez samą nazwę klasy.
Hierarchia klas wygląda następująco:
java.lang.Object ? java.util.Arrays
Geek, teraz musisz się zastanawiać, dlaczego potrzebujemy klasy Java Arrays, skoro możemy deklarować, inicjować i obliczać operacje na tablicach. Odpowiedź na to pytanie leży jednak w metodach tej klasy, które będziemy omawiać dalej, ponieważ w praktyce funkcje te pomagają programistom poszerzać horyzonty za pomocą tablic, na przykład często zdarza się, że pętle służą do wykonywania niektórych zadań na tablicy, takich jak:
- Wypełnij tablicę określoną wartością.
- Sortuj tablice.
- Szukaj w tablicach.
- I wiele więcej.
Tutaj klasa Arrays udostępnia kilka metod statycznych, których można użyć do bezpośredniego wykonania tych zadań bez użycia pętli, dzięki czemu nasz kod jest bardzo krótki i zoptymalizowany.
Składnia: Deklaracja klasy
public class Arrays extends Object
Składnia: Aby móc korzystać z Arrays
Arrays.;
Metody w klasie Java Array
Klasa Arrays pakietu java.util zawiera kilka metod statycznych, których można używać do wypełniania, sortowania, wyszukiwania itp. w tablicach. Omówmy teraz metody tej klasy, które przedstawiono poniżej w formie tabelarycznej w następujący sposób:
| Metody | Przedsięwzięcie wykonane |
|---|---|
| jakoLista() | Zwraca listę o stałym rozmiarze, wspieraną przez określone Arrays |
| wyszukiwanie binarne() | Wyszukuje określony element w tablicy za pomocą algorytmu wyszukiwania binarnego |
| binarySearch(tablica, fromIndex, toIndex, klucz, komparator) | Przeszukuje zakres określonej tablicy dla określonego obiektu przy użyciu algorytmu wyszukiwania binarnego |
| porównaj (tablica 1, tablica 2) | Porównuje dwie tablice przekazane jako parametry leksykograficznie. |
| copyOf(oryginalna tablica, nowa długość) | Kopiuje określoną tablicę, obcinając ją lub dopełniając wartością domyślną (jeśli to konieczne), aby kopia miała określoną długość. |
| copyOfRange(originalArray, fromIndex, endIndex) | Kopiuje określony zakres określonej tablicy do nowego Arrays. |
| deepEquals(Obiekt[] a1, Obiekt[] a2) | Zwraca wartość true, jeśli dwie określone tablice są sobie głęboko równe. |
| deepHashCode(Obiekt[] a) | Zwraca kod skrótu oparty na głębokiej zawartości określonych Arrays. |
| deepToString(Obiekt[] a) | Zwraca ciąg znaków reprezentujący głęboką zawartość określonych Arrays. |
| równa się (tablica1, tablica2) | Sprawdza, czy obie tablice są równe, czy nie. |
| fill(originalArray, fillValue) | Przypisuje tę wartość wypełnienia do każdego indeksu tej tablicy. |
| hashCode(oryginalna tablica) | Zwraca liczbę całkowitą hashCode tej instancji tablicy. |
| niedopasowanie (tablica1, tablica2) | Znajduje i zwraca indeks pierwszego niedopasowanego elementu między dwiema określonymi tablicami. |
| równoległyPrefix(originalArray, fromIndex, endIndex, operator funkcjonalny) | Wykonuje ParalePrefix dla danego zakresu tablicy z określonym operatorem funkcjonalnym. |
| równoległyPrefix(oryginalna tablica, operator) | Wykonuje ParalePrefix dla pełnej tablicy z określonym operatorem funkcjonalnym. |
| równoległySetAll(oryginalna tablica, generator funkcjonalny) | Ustawia wszystkie elementy tej tablicy równolegle, korzystając z dostarczonej funkcji generatora. |
| sortowanie równoległe(oryginalna tablica) | Sortuje określoną tablicę przy użyciu sortowania równoległego. |
| setAll(oryginalna tablica, generator funkcjonalny) | Ustawia wszystkie elementy określonej tablicy przy użyciu dostarczonej funkcji generatora. |
| sortuj(oryginalna tablica) | Sortuje całą tablicę w kolejności rosnącej. |
| sort(originalArray, fromIndex, endIndex) | Sortuje określony zakres tablicy w kolejności rosnącej. |
| sort(T[] a, int z indeksu, int do indeksu, komparator c) | Sortuje określony zakres określonej tablicy obiektów zgodnie z kolejnością indukowaną przez określony komparator. |
| sort(T[] a, Komparator c) | Sortuje określoną tablicę obiektów zgodnie z kolejnością indukowaną przez określony komparator. |
| rozdzielacz (oryginalna tablica) | Zwraca Spliterator obejmujący wszystkie określone tablice. |
| spliterator(originalArray, fromIndex, endIndex) | Zwraca Spliterator typu tablicy obejmującej określony zakres określonych tablic. |
| strumień (oryginalna tablica) | Zwraca strumień sekwencyjny, którego źródłem jest określona tablica. |
| toString(oryginalna tablica) | Zwraca ciąg znaków reprezentujący zawartość tej tablicy. Reprezentacja łańcuchowa składa się z listy elementów tablicy ujętych w nawiasy kwadratowe ([]). Sąsiednie elementy oddzielane są znakami przecinka i spacji. Elementy są konwertowane na ciągi znaków za pomocą funkcji String.valueOf(). |
Realizacja:
Przykład 1: jakoLista() metoda
Jawa
// Java Program to Demonstrate Arrays Class> // Via asList() method> > // Importing Arrays utility class> // from java.util package> import> java.util.Arrays;> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String[] args)> > {> > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To convert the elements as List> > System.out.println(> 'Integer Array as List: '> > + Arrays.asList(intArr));> > }> }> |
Wyjście
Integer Array as List: [[I@2f4d3709]
Przykład 2: wyszukiwanie binarne() metoda
Metody te wyszukują określony element w tablicy za pomocą algorytmu wyszukiwania binarnego.
Jawa
// Java Program to Demonstrate Arrays Class> // Via binarySearch() method> > // Importing Arrays utility class> // from java.util package> import> java.util.Arrays;> > // Main class> public> class> GFG {> > > // Main driver method> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > Arrays.sort(intArr);> > > int> intKey => 22> ;> > > // Print the key and corresponding index> > System.out.println(> > intKey +> ' found at index = '> > + Arrays.binarySearch(intArr, intKey));> > }> }> |
Wyjście
22 found at index = 3
Przykład 3: binarySearch(array, fromIndex, toIndex, key, Comparator) Metoda
Ta metoda przeszukuje zakres określonej tablicy dla określonego obiektu przy użyciu algorytmu wyszukiwania binarnego.
Jawa
// Java program to demonstrate> // Arrays.binarySearch() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > Arrays.sort(intArr);> > > int> intKey => 22> ;> > > System.out.println(> > intKey> > +> ' found at index = '> > + Arrays> > .binarySearch(intArr,> 1> ,> 3> , intKey));> > }> }> |
Wyjście
22 found at index = -4
Przykład 4: porównaj(tablica 1, tablica 2) Metoda
Jawa
// Java program to demonstrate> // Arrays.compare() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // Get the second Array> > int> intArr1[] = {> 10> ,> 15> ,> 22> };> > > // To compare both arrays> > System.out.println(> 'Integer Arrays on comparison: '> > + Arrays.compare(intArr, intArr1));> > }> }> |
Wyjście
Integer Arrays on comparison: 1
Przykład 5: porównajUnsigned(tablica 1, tablica 2) Metoda
Jawa
// Java program to demonstrate> // Arrays.compareUnsigned() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Arrays> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // Get the second Arrays> > int> intArr1[] = {> 10> ,> 15> ,> 22> };> > > // To compare both arrays> > System.out.println(> 'Integer Arrays on comparison: '> > + Arrays.compareUnsigned(intArr, intArr1));> > }> }> |
Wyjście
Integer Arrays on comparison: 1
Przykład 6: Metoda copyOf(originalArray, newLength).
Jawa
// Java program to demonstrate> // Arrays.copyOf() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To print the elements in one line> > System.out.println(> 'Integer Array: '> > + Arrays.toString(intArr));> > > System.out.println(> '
New Arrays by copyOf:
'> );> > > System.out.println(> 'Integer Array: '> > + Arrays.toString(> > Arrays.copyOf(intArr,> 10> )));> > }> }> |
Wyjście
Integer Array: [10, 20, 15, 22, 35] New Arrays by copyOf: Integer Array: [10, 20, 15, 22, 35, 0, 0, 0, 0, 0]
Przykład 7: copyOfRange(originalArray, fromIndex, endIndex) Metoda
Jawa
// Java program to demonstrate> // Arrays.copyOfRange() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To print the elements in one line> > System.out.println(> 'Integer Array: '> > + Arrays.toString(intArr));> > > System.out.println(> '
New Arrays by copyOfRange:
'> );> > > // To copy the array into an array of new length> > System.out.println(> 'Integer Array: '> > + Arrays.toString(> > Arrays.copyOfRange(intArr,> 1> ,> 3> )));> > }> }> |
Wyjście
Integer Array: [10, 20, 15, 22, 35] New Arrays by copyOfRange: Integer Array: [20, 15]
Przykład 8: deepEquals(Obiekt[] a1, Obiekt[] a2) Metoda
Jawa
// Java program to demonstrate> // Arrays.deepEquals() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Arrays> > int> intArr[][] = { {> 10> ,> 20> ,> 15> ,> 22> ,> 35> } };> > > // Get the second Arrays> > int> intArr1[][] = { {> 10> ,> 15> ,> 22> } };> > > // To compare both arrays> > System.out.println(> 'Integer Arrays on comparison: '> > + Arrays.deepEquals(intArr, intArr1));> > }> }> |
Wyjście
Integer Arrays on comparison: false
Przykład 9: deepHashCode(Object[] a) Metoda
Jawa
// Java program to demonstrate> // Arrays.deepHashCode() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[][] = { {> 10> ,> 20> ,> 15> ,> 22> ,> 35> } };> > > // To get the dep hashCode of the arrays> > System.out.println(> 'Integer Array: '> > + Arrays.deepHashCode(intArr));> > }> }> |
Wyjście
Integer Array: 38475344
Przykład 10: deepToString(Object[] a) Metoda
Jawa
// Java program to demonstrate> // Arrays.deepToString() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[][] = { {> 10> ,> 20> ,> 15> ,> 22> ,> 35> } };> > > // To get the deep String of the arrays> > System.out.println(> 'Integer Array: '> > + Arrays.deepToString(intArr));> > }> }> |
Wyjście
Integer Array: [[10, 20, 15, 22, 35]]
Przykład 11: równa się (tablica1, tablica2) Metoda
Jawa
// Java program to demonstrate> // Arrays.equals() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Arrays> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // Get the second Arrays> > int> intArr1[] = {> 10> ,> 15> ,> 22> };> > > // To compare both arrays> > System.out.println(> 'Integer Arrays on comparison: '> > + Arrays.equals(intArr, intArr1));> > }> }> |
Wyjście
Integer Arrays on comparison: false
Przykład 12: fill(originalArray, fillValue) metoda
Jawa
// Java program to demonstrate> // Arrays.fill() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Arrays> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > int> intKey => 22> ;> > > Arrays.fill(intArr, intKey);> > > // To fill the arrays> > System.out.println(> 'Integer Array on filling: '> > + Arrays.toString(intArr));> > }> }> |
Wyjście
Integer Array on filling: [22, 22, 22, 22, 22]
Przykład 13: Metoda hashCode(originalArray).
Jawa
// Java program to demonstrate> // Arrays.hashCode() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To get the hashCode of the arrays> > System.out.println(> 'Integer Array: '> > + Arrays.hashCode(intArr));> > }> }> |
Wyjście
Integer Array: 38475313
Przykład 14: mismatch(tablica1, tablica2) Metoda
Jawa
// Java program to demonstrate> // Arrays.mismatch() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Arrays> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // Get the second Arrays> > int> intArr1[] = {> 10> ,> 15> ,> 22> };> > > // To compare both arrays> > System.out.println(> 'The element mismatched at index: '> > + Arrays.mismatch(intArr, intArr1));> > }> }> |
Wyjście
The element mismatched at index: 1
Przykład 15: Metoda równoległego sortowania(originalArray).
Jawa
// Java program to demonstrate> // Arrays.parallelSort() method> > // Importing Arrays class from> // java.util package> import> java.util.Arrays;> > // Main class> public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To sort the array using parallelSort> > Arrays.parallelSort(intArr);> > > System.out.println(> 'Integer Array: '> > + Arrays.toString(intArr));> > }> }> |
Wyjście
Integer Array: [10, 15, 20, 22, 35]
Przykład 16: sortuj(oryginalna tablica) metoda
Jawa
// Java program to demonstrate> // Arrays.sort() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To sort the array using normal sort-> > Arrays.sort(intArr);> > > System.out.println(> 'Integer Array: '> > + Arrays.toString(intArr));> > }> }> |
Wyjście
Integer Array: [10, 15, 20, 22, 35]
Przykład 17: sort(originalArray, fromIndex, endIndex) metoda
Jawa
// Java program to demonstrate> // Arrays.sort() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To sort the array using normal sort> > Arrays.sort(intArr,> 1> ,> 3> );> > > System.out.println(> 'Integer Array: '> > + Arrays.toString(intArr));> > }> }> |
Wyjście
Integer Array: [10, 15, 20, 22, 35]
Przykład 18: sort(T[] a, int fromIndex, int toIndex, Komparator c) metoda
Jawa
// Java program to demonstrate working of Comparator> // interface> import> java.util.*;> import> java.lang.*;> import> java.io.*;> > // A class to represent a student.> class> Student {> > int> rollno;> > String name, address;> > > // Constructor> > public> Student(> int> rollno, String name,> > String address)> > {> > this> .rollno = rollno;> > this> .name = name;> > this> .address = address;> > }> > > // Used to print student details in main()> > public> String toString()> > {> > return> this> .rollno +> ' '> > +> this> .name +> ' '> > +> this> .address;> > }> }> > class> Sortbyroll> implements> Comparator {> > // Used for sorting in ascending order of> > // roll number> > public> int> compare(Student a, Student b)> > {> > return> a.rollno - b.rollno;> > }> }> > // Driver class> class> Main {> > public> static> void> main(String[] args)> > {> > Student[] arr = {> new> Student(> 111> ,> 'bbbb'> ,> 'london'> ),> > new> Student(> 131> ,> 'aaaa'> ,> 'nyc'> ),> > new> Student(> 121> ,> 'cccc'> ,> 'jaipur'> ) };> > > System.out.println(> 'Unsorted'> );> > for> (> int> i => 0> ; i System.out.println(arr[i]); Arrays.sort(arr, 1, 2, new Sortbyroll()); System.out.println('
Sorted by rollno'); for (int i = 0; i System.out.println(arr[i]); } }> |
Wyjście
Unsorted 111 bbbb london 131 aaaa nyc 121 cccc jaipur Sorted by rollno 111 bbbb london 131 aaaa nyc 121 cccc jaipur
Przykład 19: sort(T[] a, Komparator c) metoda
Jawa
// Java program to demonstrate working of Comparator> // interface> import> java.util.*;> import> java.lang.*;> import> java.io.*;> > // A class to represent a student.> class> Student {> > int> rollno;> > String name, address;> > > // Constructor> > public> Student(> int> rollno, String name,> > String address)> > {> > this> .rollno = rollno;> > this> .name = name;> > this> .address = address;> > }> > > // Used to print student details in main()> > public> String toString()> > {> > return> this> .rollno +> ' '> > +> this> .name +> ' '> > +> this> .address;> > }> }> > class> Sortbyroll> implements> Comparator {> > > // Used for sorting in ascending order of> > // roll number> > public> int> compare(Student a, Student b)> > {> > return> a.rollno - b.rollno;> > }> }> > // Driver class> class> Main {> > public> static> void> main(String[] args)> > {> > Student[] arr = {> new> Student(> 111> ,> 'bbbb'> ,> 'london'> ),> > new> Student(> 131> ,> 'aaaa'> ,> 'nyc'> ),> > new> Student(> 121> ,> 'cccc'> ,> 'jaipur'> ) };> > > System.out.println(> 'Unsorted'> );> > for> (> int> i => 0> ; i System.out.println(arr[i]); Arrays.sort(arr, new Sortbyroll()); System.out.println('
Sorted by rollno'); for (int i = 0; i System.out.println(arr[i]); } }> |
Wyjście
Unsorted 111 bbbb london 131 aaaa nyc 121 cccc jaipur Sorted by rollno 111 bbbb london 121 cccc jaipur 131 aaaa nyc
Przykład 20: Metoda spliterator(originalArray).
Jawa
// Java program to demonstrate> // Arrays.spliterator() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To sort the array using normal sort> > System.out.println(> 'Integer Array: '> > + Arrays.spliterator(intArr));> > }> }> |
Wyjście
Integer Array: java.util.Spliterators$IntArraySpliterator@4e50df2e
Przykład 21: spliterator(originalArray, fromIndex, endIndex) Metoda
Jawa
// Java program to demonstrate> // Arrays.spliterator() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To sort the array using normal sort> > System.out.println(> 'Integer Array: '> > + Arrays.spliterator(intArr,> 1> ,> 3> ));> > }> }> |
Wyjście
Integer Array: java.util.Spliterators$IntArraySpliterator@4e50df2e
Przykład 22: Metoda stream(originalArray).
Jawa
// Java program to demonstrate> // Arrays.stream() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To get the Stream from the array> > System.out.println(> 'Integer Array: '> > + Arrays.stream(intArr));> > }> }> |
Wyjście
Integer Array: java.util.stream.IntPipeline$Head@7291c18f
Przykład 23: toString(oryginalna tablica) metoda
Jawa
// Java program to demonstrate> // Arrays.toString() method> > import> java.util.Arrays;> > public> class> Main {> > public> static> void> main(String[] args)> > {> > > // Get the Array> > int> intArr[] = {> 10> ,> 20> ,> 15> ,> 22> ,> 35> };> > > // To print the elements in one line> > System.out.println(> 'Integer Array: '> > + Arrays.toString(intArr));> > }> }> |
Wyjście
Integer Array: [10, 20, 15, 22, 35]