Chương trình Java để truy cập các phần tử từ LinkedList.

Trong ví dụ này, chúng ta sẽ học cách truy cập các phần tử từ LinkedList trong Java bằng nhiều phương pháp khác nhau.

Ví dụ 1: Truy cập các phần tử từ danh sách liên kết

 import java.util.LinkedList; class Main ( public static void main(String() args) ( LinkedList languages = new LinkedList(); // add elements in the LinkedList languages.add("Python"); languages.add("Java"); languages.add("JavaScript"); System.out.println("LinkedList: " + languages); // get the element from the LinkedList String str = languages.get(1); System.out.print("Element at index 1: " + str); ) )

Đầu ra

 LinkedList: (Python, Java, JavaScript) Phần tử ở chỉ mục 1: Java

Trong ví dụ trên, chúng ta đã sử dụng get()phương thức với tham số 1 . Ở đây, phương thức trả về phần tử ở chỉ mục 1 .

Ví dụ 2: Sử dụng phương thức iterator ()

Chúng ta cũng có thể sử dụng phương thức iterator () để lặp qua các phần tử của danh sách liên kết. Chúng ta phải nhập java.util.Iteratorgói để sử dụng phương pháp này. Ví dụ,

 import java.util.LinkedList; import java.util.Iterator; class Main ( public static void main(String() args) ( LinkedList animals= new LinkedList(); // Add elements in LinkedList animals.add("Dog"); animals.add("Horse"); animals.add("Cat"); // Creating an object of Iterator Iterator iterate = animals.iterator(); System.out.print("LinkedList: "); while(iterate.hasNext()) ( System.out.print(iterate.next()); System.out.print(", "); ) ) )

Đầu ra

 LinkedList: Dog, Cat, Horse,

Đây,

  • hasNext()- trả về truenếu có phần tử tiếp theo
  • next() - trả về phần tử tiếp theo

Để tìm hiểu thêm Iterator, hãy truy cập Giao diện lặp lại Java.

Ví dụ 3: Sử dụng phương thức listIterator ()

Chúng ta cũng có thể sử dụng listIterator()phương thức này để lặp lại các phần tử của LinkedList. Để sử dụng phương pháp này, chúng ta phải nhập java.util.ListIteratorgói.

 import java.util.LinkedList; import java.util.ListIterator; class Main ( public static void main(String() args) ( LinkedList animals= new LinkedList(); // Add elements in LinkedList animals.add("Dog"); animals.add("Horse"); animals.add("Cat"); // Create an object of ListIterator ListIterator listIterate = animals.listIterator(); System.out.print("LinkedList: "); while(listIterate.hasNext()) ( System.out.print(listIterate.next()); System.out.print(", "); ) // Iterate backward System.out.print("Reverse LinkedList: "); while(listIterate.hasPrevious()) ( System.out.print(listIterate.previous()); System.out.print(", "); ) ) )

Đầu ra

 LinkedList: Dog, Horse, Cat, Reverse LinkedList: Cat, Horse, Dog,

Đây,

  • hasNext()- trả về truenếu có phần tử tiếp theo
  • next() - trả về phần tử tiếp theo
  • hasPrevious()- trả về truenếu tồn tại các phần tử trước đó
  • previous() - trả về phần tử trước đó

Để tìm hiểu thêm về ListIterator, hãy truy cập Giao diện Java ListIterator.

Lưu ý : listIterator()Phương pháp được ưu tiên hơn iterator(). Điều này là do nó cũng cho phép bạn lặp lại.

thú vị bài viết...