> Java > java지도 시간 > 본문

자바의 LinkedList

PHPz
풀어 주다: 2024-08-30 15:48:16
원래의
751명이 탐색했습니다.

Java의 LinkedList는 배열과 다른 선형 데이터 구조입니다. Java 프로그램에서 Linked List를 사용하면 장점과 단점이 있습니다. 링크드리스트의 각 요소는 노드(Node)라는 셀에 저장됩니다. 각 노드에는 특정 주소가 있습니다. LinkedList의 가장 큰 단점은 각 지점에서 노드에 쉽게 접근할 수 없다는 것입니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

각 노드의 주소를 지정하는 데 사용되는 포인터가 있으며, 특정 노드에 액세스하려면 헤드부터 시작해야 노드가 액세스할 특정 포인터에 도달합니다. LinkedList 클래스도 다른 Java 인터페이스와 마찬가지로 많은 생성자와 메소드로 구성됩니다. 이번 글에서는 LinkedList에서 사용되는 두 개의 생성자를 살펴보겠습니다.

다음과 같습니다.

  • LinkedList(): 빈 LinkedList()를 할당하는 데 사용됩니다.
  • LinkedList(컬렉션 C): 이는 컬렉션의 반복자가 반환한 대로 지정된 컬렉션의 모든 요소를 ​​포함하는 순서가 지정된 목록을 만드는 데 사용됩니다.

Java의 LinkedList 메서드

Java LinkedList 클래스의 일부인 많은 메서드나 함수가 있습니다. 이 기사에서는 Java LinkedList 클래스의 일부 기능을 살펴보겠습니다.

다음과 같습니다.

  • add(int a, I Element): 이 메소드는 목록의 특정 위치에 특정 요소를 삽입하는 데 사용됩니다.
  • add( E e): 이 메소드는 지정된 요소를 목록의 끝 부분에 고정합니다.
  • add(int index, Collection C): 이 메소드는 시작 위치부터 시작하여 목록에 지정된 모든 요소를 ​​삽입합니다.
  • offerFirst​(): 이 메서드는 지정된 요소를 이 목록 앞에 삽입합니다.
  • addLast(): 이 메소드는 목록 끝에 요소를 삽입하는 데 사용됩니다.
  • voidclear(): 이 메소드는 Linkedlist에서 모든 요소를 ​​제거하는 데 사용됩니다.
  • poll​(): 목록의 첫 번째 요소를 제거합니다.
  • lastIndexOf(): 이 목록에서 지정된 요소가 마지막으로 나타나는 인덱스를 반환하는 데 사용됩니다.
  • getLast(): 이 함수는 LinkedList의 마지막 요소를 반환하는 데 사용됩니다.
  • offer​(): 이 메소드는 지정된 요소를 목록의 꼬리 요소로 삽입합니다.
  • offerLast​(): 이 메서드는 이 목록의 끝에 지정된 요소를 삽입합니다.
  • peek​(): 목록의 첫 번째 요소를 검색합니다.
  • peekFirst​(): 이 메소드는 목록의 마지막 요소를 검색하거나 목록이 비어 있는 경우 null을 반환하는 데 사용됩니다.
  • addFirst(): 이 메소드는 목록의 시작 부분에 요소를 삽입하는 데 사용됩니다.
  • peekLast​(): 이 메소드는 목록의 마지막 요소를 검색하는 데 사용되거나 목록이 비어 있는 경우 null을 반환합니다.
  • pollFirst​(): 이 메소드는 이 목록의 첫 번째 요소를 검색하고 제거하는 데 사용되며, 이 목록이 비어 있으면 null을 반환합니다.
  • contains(): 이 함수는 LinkedList에 노드의 특정 요소가 포함된 경우 true를 반환합니다.
  • pollLast​(): 이 메소드는 이 목록의 마지막 요소를 제거하거나 이 목록이 비어 있으면 null을 반환합니다.
  • removeFirst​(): 이 메소드는 이 목록의 첫 번째 요소를 반환합니다.
  • element(): 이 메소드는 목록의 헤드를 검색하지만 제거하지는 않습니다.
  • getFirst(): 이 메소드는 LinkedList의 첫 번째 요소를 반환하는 데 사용됩니다.
  • remove​(): 이 메소드는 LinkedList의 첫 번째 요소를 제거합니다.
  • remove​(int index): 이 메소드는 이 목록의 지정된 위치에 있는 요소를 제거합니다.
  • removeLast​(): 이 메소드는 이 목록의 마지막 요소를 반환합니다.
  • set​(int index, E element): 이 메서드는 이 목록의 지정된 위치에 있는 요소를 지정된 요소로 바꿉니다.
  • size​(): 이 메서드는 이 목록의 요소 수를 반환합니다.

Java의 LinkedList 예

다음은 언급된 예입니다.

예시 #1

이 코딩 예제에서는 연결 목록에 특정 요소를 삽입한 다음 제거하고 마지막으로 연결 목록을 표시하는 LinkedList 메서드를 살펴보겠습니다.

코드:

import java.util.*;
public class Example3
{
public static void main(String args[])
{
LinkedList<String> object = new LinkedList<String>();
// Adding elements to the linked list
object.add("A");
object.add("B");
object.addLast("C");
object.addFirst("D");
object.add(2, "E");
object.add("F");
object.add("G");
System.out.println("Linked list : " + object);
object.remove("C");
object.remove(3);
object.removeFirst();
object.removeLast();
System.out.println("Linked list after deletion: " + object);
}
}
로그인 후 복사

출력:

자바의 LinkedList

In the sample output, we see that there are certain elements in the linkedlist, and finally, certain elements are deleted, and then the linkedlist after all the deletion of the elements is shown.

Example #2

In this program, we are going to see four names being printed using sequential order in LinkedList. We use a String LinkedList and use it to print names that can be of any number. We use the While loop here for printing the names which are present in the program.

Code:

import java.util.*;
public class LinkedList1
{
public static void main(String args[])
{
LinkedList<String> al=new LinkedList<String>();
al.add("Ishankamal Mitra");
al.add("Sourya Mukherjee");
al.add("Satyaki Das");
al.add("Debodooty Sarkar");
Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
로그인 후 복사

Output:

자바의 LinkedList

In this program, we check how the coding helps us to print four names in sequential order as mentioned in the LinkedList. In the next program, we are going to see how the sequence is changed; that is, the names are printed in reverse order of the input.

Example #3

In this code, the program inputs the name and then prints the names in the reverse order of their sequence.

Code:

import java.util.*;
public class LinkedList4
{
public static void main(String args[])
{
LinkedList<String> ll=new LinkedList<String>();
ll.add("Ishankamal Mitra");
ll.add("Sourya Mukherjee");
ll.add("Satyaki Das");
//Going through the list of elements in Reverse order
Iterator i=ll.descendingIterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
로그인 후 복사

Output:

자바의 LinkedList

In this program, we use the DescendingIterator(), and we use it to print the names in the reverse order of the input. We can see it very clearly through the program.

Conclusion

In this article, we saw the different constructors and methods which are present in the LinkedList class. Plus, we saw a Java program to illustrate the insertion and deletion of elements in a LinkedList. We also saw the advantages and disadvantages of using LinkedList over arrays. They contain nodes that are not easily accessible and have to be accessed through the LinkedList head. We also notice three examples of coding where names are printed in reverse order, sequential order, and removing elements from a LinkedList. These programs help us to understand the methodology of the LinkedList class.

위 내용은 자바의 LinkedList의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!