logo

Kako stvoriti generički popis u Javi?

Java je moćan programski jezik koji se može koristiti za stvaranje širokog niza desktop, online i mobilnih aplikacija. Sučelje List je jedna od Javinih temeljnih struktura podataka. Popis je skupina elemenata koji su raspoređeni određenim redoslijedom i mogu uključivati ​​duplikate. U ovom vodiču ćemo pogledati kako stvoriti generički popis u Javi.

Što je generički popis u Javi?

Generički popis u Javi je grupiranje elemenata određene vrste. ArrayList, LinkedList i Vector samo su neke od klasa koje implementiraju opće sučelje List, koje je navedeno u paketu java.util. Programeri mogu stvoriti kolekciju komponenti bilo koje vrste i iskoristiti sigurnost tipa tijekom kompajliranja korištenjem generičkog popisa.

Stvaranje generičkog popisa u Javi

Da bismo stvorili generički popis u Javi, moramo slijediti ove korake:

Korak 1: Uvezite potrebne pakete

U Javi moramo uvesti paket java.util da bismo koristili sučelje List.

Korak 2: Objavite popis

Popis se zatim mora deklarirati korištenjem opće sintakse. U uglastim zagradama (>) opisujemo vrstu elemenata koje želimo pohraniti na popis.

Za izradu popisa brojeva, na primjer, možemo ga deklarirati na sljedeći način:

 List integerList = new ArrayList(); 

Ovo deklarira popis pod nazivom integerList koji može pohraniti elemente tipa Integer. Koristili smo klasu ArrayList za implementaciju sučelja List.

Korak 3: Dodajte elemente na popis

Nakon što deklariramo Listu, možemo joj dodati elemente pomoću metode add().

Na primjer, da bismo dodali cijeli broj integerList-u, koristili bismo sljedeći kod:

java lista
 integerList.add(10); 

Dodaje vrijednost cijelog broja 10 na kraj popisa.

Korak 4: Pristup elementima Liste

Elementima Liste možemo pristupiti metodom get().

Na primjer, da bismo dobili prvi element liste integerList, koristili bismo sljedeći kod:

 int firstElement = integerList.get(0); 

Ovo dohvaća prvi element popisa i pohranjuje ga u varijablu firstElement.

Korak 5: Iterirajte po popisu

Možemo iterirati po elementima Liste koristeći for petlju.

Na primjer, da bismo ispisali sve elemente integerList-a, koristili bismo sljedeći kod:

 for (int i = 0; i <integerlist.size(); i++) { system.out.println(integerlist.get(i)); } < pre> <p>It iteratively prints each element of the List to the console.</p> <p>Any Java developer must possess the fundamental ability to create a generic List in Java. You can create a List of any type and carry out activities like adding and accessing entries as well as iterating over the List by following the instructions provided in this article. Generic Lists offer type safety and enable more robust and maintainable programming.</p> <p>TheJava programme builds a generic List of strings, elements are added to it, and the result is printed to the console. The user is prompted to enter each string when the programme asks for their input on how many strings to add to the list.</p> <p> <strong>GenericListExample.java</strong> </p> <pre> import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class GenericListExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&apos;How many strings do you want to add to the list? &apos;); int numStrings = scanner.nextInt(); List stringList = new ArrayList(); for (int i = 0; i <numstrings; i++) { system.out.print('enter string ' + (i+1) ': '); inputstring="scanner.next();" stringlist.add(inputstring); } system.out.println('the strings in the list are:'); for (string str : stringlist) system.out.println(str); < pre> <p> <strong>Output:</strong> </p> <pre> How many strings do you want to add to the list? 3 Enter string 1: apple Enter string 2: banana Enter string 3: orange The strings in the list are: apple banana orange </pre> <p>In this example, the user inputs 3 as the number of strings to add to the list. The user is then prompted by the programme to enter each string individually. The programme prints each string in the list to the console when all strings have been added to the list.</p> <h2>Advantages of Using Generic Lists</h2> <p>Using generic Lists in Java has several advantages:</p> <p> <strong>Type safety:</strong> By specifying the type of elements that the List can contain; we can avoid runtime errors caused by adding elements of the wrong type.</p> <p> <strong>Reusability:</strong> Generic Lists can be used with any type of element, making them a flexible and reusable data structure.</p> <p> <strong>Performance:</strong> Many List implementations, such as ArrayList, offer fast access and modification times.</p> <p> <strong>Familiarity:</strong> Lists are a commonly used data structure in computer science, making them a familiar and intuitive choice for many developers.</p> <hr></numstrings;></pre></integerlist.size();>

U ovom primjeru, korisnik unosi 3 kao broj nizova za dodavanje na popis. Program zatim od korisnika traži da unese svaki niz pojedinačno. Program ispisuje svaki niz na popisu na konzolu kada su svi nizovi dodani na popis.

Prednosti korištenja generičkih popisa

Korištenje generičkih popisa u Javi ima nekoliko prednosti:

Vrsta sigurnosti: Određivanjem vrste elemenata koje Popis može sadržavati; možemo izbjeći pogreške tijekom izvođenja uzrokovane dodavanjem elemenata pogrešne vrste.

Ponovno korištenje: Generički popisi mogu se koristiti s bilo kojom vrstom elementa, što ih čini fleksibilnom strukturom podataka koja se može ponovno koristiti.

Izvođenje: Mnoge implementacije popisa, kao što je ArrayList, nude brz pristup i vrijeme izmjene.

Familijarnost: Popisi su često korištena struktura podataka u računalnoj znanosti, što ih čini poznatim i intuitivnim izborom za mnoge programere.