java.util.LinkedList.removeFirstOccurrence()用于从列表中删除第一次出现的指定元素。如果没有出现指定的元素,则列表保持不变。
用法:
LinkedListObject.removeFirstOccurrence(Object element)
参数:该方法采用一个参数element ,该参数element 将从列表中删除。对象类型应与列表中的元素相同。
返回值:如果列表包含指定的元素并被删除,则该方法返回boolean True,否则返回false。
以下示例程序旨在说明removeFirstOccurrence()方法:
示例1:
// Java code to demonstrate removeFirstOccurrence() method
import java.util.LinkedList;
public class GfG {
// Main method
public static void main(String[] args)
{
// Creating a LinkedList objec
LinkedList<String> list = new LinkedList<String>();
// Adding an element at the last
list.addLast("one");
// Adding an element at the last
list.addLast("two");
// Adding an element at the last
list.addLast("three");
// Adding an element at the last
list.addLast("one");
System.out.print("List before removing the "+
"first Occurrence of \"one\" : ");
// Printing the list
System.out.println(list);
// Removing first occurrence of one.
boolean returnValue = list.removeFirstOccurrence("one");
// Printing the returned value
System.out.println("Returned Value : " + returnValue);
System.out.print("List after removing the"+
" first Occurrence of \"one\" : ");
// Printing the list
System.out.println(list);
}
}
输出:
List before removing the first Occurrence of "one" : [one, two, three, one] Returned Value : true List after removing the first Occurrence of "one" : [two, three, one]
示例2:
// Java code to demonstrate removeFirstOccurrence method in LinkedList
import java.util.LinkedList;
public class GfG {
// Main method
public static void main(String[] args)
{
// Creating a LinkedList objec
LinkedList<Integer> list = new LinkedList<Integer>();
// Adding an element at the last
list.addLast(10);
// Adding an element at the last
list.addLast(20);
// Adding an element at the last
list.addLast(30);
// Adding an element at the last
list.addLast(10);
System.out.print("List before removing the"+
" first Occurrence of \"10\" : ");
// Printing the list
System.out.println(list);
// Removing first occurrence of one.
boolean returnValue = list.removeFirstOccurrence(10);
// Printing the returned value
System.out.println("Returned Value : " + returnValue);
System.out.print("List after removing the"+
" first Occurrence of \"10\" : ");
// Printing the list
System.out.println(list);
}
}
输出:
List before removing the first Occurrence of "10" : [10, 20, 30, 10] Returned Value : true List after removing the first Occurrence of "10" : [20, 30, 10]
相关用法
- Java ConcurrentLinkedDeque removeFirstOccurrence()用法及代码示例
- Java LinkedBlockingDeque removeFirstOccurrence()用法及代码示例
- Java ArrayDeque removeFirstOccurrence()用法及代码示例
- Java BlockingDeque removeFirstOccurrence()用法及代码示例
- Java LinkedList pop()用法及代码示例
- Java LinkedList get()用法及代码示例
- Java LinkedList add()用法及代码示例
- Java LinkedList set()用法及代码示例
- Java LinkedList contains()用法及代码示例
- Java LinkedList removeLast()用法及代码示例
- Java LinkedList lastIndexOf()用法及代码示例
- Java LinkedList clone()用法及代码示例
- Java LinkedList indexOf()用法及代码示例
- Java LinkedList spliterator()用法及代码示例
- Java LinkedList size()用法及代码示例
注:本文由纯净天空筛选整理自ShivamKD大神的英文原创作品 LinkedList removeFirstOccurrence() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。