logo

כיצד ליצור רשימה כללית ב-Java?

Java היא שפת תכנות חזקה שניתן להשתמש בה כדי ליצור מגוון עצום של אפליקציות שולחן עבודה, מקוונות וניידים. ממשק הרשימה הוא אחד ממבני הליבה של Java. רשימה היא קבוצה של אלמנטים המסודרים בסדר מסוים ועשויים לכלול כפילויות. נבחן כיצד ליצור רשימה גנרית ב-Java במדריך זה.

מהי רשימה כללית ב-Java?

רשימה גנרית ב-Java היא קיבוץ של אלמנטים מסוג מסוים. ArrayList, LinkedList ו-Vector הם רק כמה מהמחלקות שמיישמות את ממשק ה- List הכללי, המצוין בחבילת java.util. מפתחים יכולים ליצור אוסף של רכיבים מכל סוג ולהפיק תועלת מבטיחות סוג בזמן הקומפילציה על ידי שימוש ברשימה גנרית.

יצירת רשימה כללית ב-Java

כדי ליצור רשימה כללית ב-Java, עלינו לבצע את השלבים הבאים:

חיפוש בינארי ב-Java

שלב 1: ייבא את החבילות הנדרשות

ב-Java, עלינו לייבא את החבילה java.util כדי להשתמש בממשק List.

שלב 2: הכריז על הרשימה

לאחר מכן יש להכריז על הרשימה באמצעות התחביר הכללי. בתוך סוגריים של זווית (>), אנו מתארים את סוג האלמנטים שאנו רוצים לאחסן ברשימה.

כדי ליצור רשימה של מספרים, למשל, אנו עשויים להכריז עליה באופן הבא:

 List integerList = new ArrayList(); 

זה מכריז על רשימה בשם integerList שיכולה לאחסן אלמנטים מסוג Integer. השתמשנו במחלקה ArrayList כדי ליישם את ממשק List.

שלב 3: הוסף אלמנטים לרשימה

לאחר ההכרזה על הרשימה, נוכל להוסיף לה אלמנטים באמצעות שיטת add() .

לדוגמה, כדי להוסיף מספר שלם ל-integerList, נשתמש בקוד הבא:

תמונות סימון
 integerList.add(10); 

זה מוסיף את הערך השלם 10 לסוף הרשימה.

שלב 4: גישה לרכיבי הרשימה

אנו יכולים לגשת לאלמנטים של הרשימה באמצעות שיטת get() .

לדוגמה, כדי לקבל את האלמנט הראשון של ה-integerList, נשתמש בקוד הבא:

 int firstElement = integerList.get(0); 

זה מאחזר את האלמנט הראשון של הרשימה ומאחסן אותו במשתנה firstElement.

שלב 5: חזור על הרשימה

אנו יכולים לחזור על הרכיבים של הרשימה באמצעות לולאת for.

להשיג חיבור

לדוגמה, כדי להדפיס את כל הרכיבים של ה-integerList, נשתמש בקוד הבא:

 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();>

בדוגמה זו, המשתמש מזין 3 כמספר המחרוזות להוספה לרשימה. לאחר מכן המשתמש מתבקש על ידי התוכנית להזין כל מחרוזת בנפרד. התוכנית מדפיסה כל מחרוזת ברשימה לקונסולה כאשר כל המחרוזות נוספו לרשימה.

יתרונות השימוש ברשימות גנריות

לשימוש ברשימות גנריות ב-Java יש מספר יתרונות:

סוג בטיחות: על ידי ציון סוג האלמנטים שהרשימה יכולה להכיל; אנו יכולים להימנע משגיאות זמן ריצה הנגרמות על ידי הוספת אלמנטים מהסוג הלא נכון.

שימוש חוזר: ניתן להשתמש ברשימות כלליות עם כל סוג של אלמנט, מה שהופך אותן למבנה נתונים גמיש וניתן לשימוש חוזר.

ביצועים: יישומי List רבים, כגון ArrayList, מציעים זמני גישה ושינוי מהירים.

בְּקִיאוּת: רשימות הן מבנה נתונים נפוץ במדעי המחשב, מה שהופך אותן לבחירה מוכרת ואינטואיטיבית עבור מפתחים רבים.