Tablice w Javie różnią się pod względem implementacji i użycia w porównaniu z tablicami w C/C++, chociaż mają również wiele podobieństw. Tutaj omówimy, jak zwrócić tablicę w Javie.
przycinanie alfa beta
Aby zwrócić tablicę w Javie, musimy zadbać o następujące punkty:
Punkt kluczowy 1: Metoda zwracająca tablicę musi mieć typ zwracany jako tablicę tego samego typu danych, co zwracana tablica. Typ zwracany może być zwykłym obiektem klasy Integer, Double, Character, String lub zdefiniowanym przez użytkownika.
// Method returning an integer array. int[] methodName() {...}>
// Method returning a String array. String[] methodName() {...}>
// Method returning an array of objects of class named Students. Students[] methodName() {...}>
Kluczowy punkt 2: Modyfikatory dostępu muszą być używane dokładnie, biorąc pod uwagę użycie metody i zwracanej tablicy. Należy również wziąć pod uwagę deklaracje statyczne i niestatyczne.
// Using public access modifier and static to call the method from a static class, method or block. public static char[] methodName() {...}>
Punkt kluczowy 3: W wywołaniu metody musi znajdować się dowolna tablica zmiennych tego samego typu danych lub czegoś podobnego, aby obsłużyć zwracaną tablicę. Na przykład tablicę liczb całkowitych zwróconą przez dowolną metodę można przechowywać w następujący sposób.
int[] storage = methodReturningArray();>
Realizacja:
Aby lepiej to zrozumieć, możemy przyjrzeć się kilku różnym scenariuszom, w których możemy zwracać tablice. Tutaj rozważymy trzy przykłady scenariuszy.
- Przypadek 1: Proste tablice wbudowane
- Przypadek 2: Tablica obiektów
- Przypadek 3: Zwracanie tablic wielowymiarowych
Przypadek 1: Zwracanie tablicy liczb całkowitych (wbudowany typ danych) w Javie
Dowolna tablica wbudowanego typu danych może być liczbą całkowitą, znakiem, zmiennoprzecinkowym, podwójnym, a wszystko to można zwrócić po prostu za pomocą instrukcji return, pamiętając o punktach wymienionych powyżej.
Przykład
sumator pełny
Jawa
// Java Program to Illustrate Returning> // simple built-in arrays> // Importing input output classes> import> java.io.*;> // Main class> class> GFG {> > // Method 1> > // Main driver method> > public> static> void> main(String[] args)> > {> > // An integer array storing the returned array> > // from the method> > int> [] storage = methodReturningArray();> > // Printing the elements of the array> > for> (> int> i => 0> ; i System.out.print(storage[i] + ' '); } // Method 2 // Returning an integer array public static int[] methodReturningArray() { int[] sample = { 1, 2, 3, 4 }; // Return statement of the method. return sample; } }> |
>
>Wyjście
1 2 3 4>
Przypadek 2: Zwracanie tablicy obiektów w Javie
Odbywa się to dokładnie w podobny sposób w przypadku zwracania tablic wbudowanych typów danych.
10 milionów
Przykład
Jawa
// Java Program to Illustrate Returning> // an array of objects in java> // Importing all input output classes> import> java.io.*;> // Class 1> // Helper class> // Courses whose objects are returned as an array> class> Courses {> > String name;> > int> modules;> > // Constructor to instantiate class objects.> > public> Courses(String n,> int> m)> > {> > // This keyword refers to current instance itself> > this> .name = n;> > this> .modules = m;> > }> }> // Class 2> // Main class> class> GFG {> > // Method 1> > // Main driver method> > public> static> void> main(String[] args)> > {> > // Calling the method for returning an array of> > // objects of the Courses class.> > Courses[] sample = methodReturningArray();> > // Printing the returned array elements.> > for> (> int> i => 0> ; i System.out.print(sample[i].name + ' - ' + sample[i].modules + ' modules
'); } // Method 2 // Note that return type is an array public static Courses[] methodReturningArray() { // Declaring Array of objects of the Courses class Courses[] arr = new Courses[4]; // Custom array of objects arr[0] = new Courses('Java', 31); arr[1] = new Courses('C++', 26); arr[2] = new Courses('DSA', 24); arr[3] = new Courses('DBMS', 12); // Statement to return an array of objects return arr; } }> |
>
pasmo podstawowe vs łącze szerokopasmowe
>Wyjście
Java - 31 modules C++ - 26 modules DSA - 24 modules DBMS - 12 modules>
Przypadek 3: Zwracanie tablic wielowymiarowych
Tablice wielowymiarowe w Javie można powiedzieć, że jest tablicą tablic wewnątrz tablic. Najprostszą formą może być tablica dwuwymiarowa. Mają swoje rozmiary i deklarację według swoich rozmiarów. Poniżej zademonstrowano zwracanie tablicy dwuwymiarowej, która ma bardzo podobne podejście do tablic jednowymiarowych.
Przykład
Jawa
// Java Program to Illustrate Returning> // Multi-dimensional Arrays> // Importing input output classes> import> java.io.*;> // Main class> class> GFG {> > // Method 1> > // Main driver method> > public> static> void> main(String[] args)> > {> > // An integer 2D array storing the> > // returned array from the method> > int> [][] storage = methodReturningArray();> > // Printing the elements of the array> > // using nested for loops> > for> (> int> i => 0> ; i for (int j = 0; j 0].length; j++) System.out.print(storage[i][j] + ' '); System.out.println(); } } // Method 2 // Returning an integer array public static int[][] methodReturningArray() { // Custom 2D integer array int[][] sample = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Return statement of the method return sample; } }> |
przekonwertuj datę ciągu
>
>Wyjście
1 2 3 4 5 6 7 8 9>