📌  相关文章
📜  使用 ListIterator 从 ArrayList 中删除元素的Java程序

📅  最后修改于: 2022-05-13 01:54:41.390000             🧑  作者: Mango

使用 ListIterator 从 ArrayList 中删除元素的Java程序

ListIterator.remove() 方法从 next() 或 previous() 光标位置返回的列表中删除最后一个元素。每次调用 next 或 previous 时只能调用一次。只有在最后一次调用 next 或 previous 之后没有调用 add(E) 操作时才可以进行。

ArrayList 中的内部工作如下所示,即删除或添加元素。考虑在切换到 ListIterator 之前对元素进行通用删除。

句法:

listIterator.remove();
  1. 要删除的索引
  2. 要删除的索引值

插图:

过程:使用 ListIterator 从 ArrayList 中删除元素如下:

  1. 创建 ArrayList 实例 new ArrayList();
  2. 使用colors.add(“Red”)以ArrayList颜色添加元素;
  3. 创建 colors.listIterator() 的 ListIterator 实例;
  4. 在删除元素之前打印列表元素。
  5. 通过listIterator.next()增加迭代器并移动到要删除的元素;
  6. 通过 listIterator.remove() 删除元素;
  7. 删除元素后打印列表。在这个例子中,我们删除了元素“White.

从 ArrayList 中删除元素时的状态事务

情况 1:如果要删除的元素的索引已知,则使用循环 

Java
// Java Program to Remove an element from ArrayList
// using ListIterator
 
// Importing ArrayList and ListIterator classes
// of java.util package
import java.util.ArrayList;
import java.util.ListIterator;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Create an  ArrayList
        ArrayList colors = new ArrayList();
 
        // Add elements to above ArrayList
        colors.add("Red");
        colors.add("White");
        colors.add("Blue");
        colors.add("Pink");
        colors.add("Black");
        colors.add("Green");
 
        // ArrayList ={Red, White, Blue, Pink, Black, Green}
        ListIterator listIterator
            = colors.listIterator();
 
        System.out.println("List Before remove() method = "
                           + colors);
 
        // Removing ith element from ArrayList
        // using listiterator
 
        // Suppose i = 3, so traversing
        // till that element
        for (int i = 0; i < 3; i++) {
            listIterator.next();
        }
 
        // Removes one more element from list
        // 'blue' element is removed from arraylist
        listIterator.remove();
 
        // Printing the final ArrayList after removing
        // elements from originally created ArrayList
        System.out.println("List After remove() method =  "
                           + colors);
    }
}


Java
// Java Program to Remove an element from ArrayList
// using ListIterator
 
// Importing ArrayList and ListIterator classes
// of java.util package
import java.util.ArrayList;
import java.util.ListIterator;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Create an ArrayList
        ArrayList colors = new ArrayList();
 
        // Adding elements to the arraylist
        colors.add("Red");
        colors.add("White");
        colors.add("Blue");
        colors.add("Pink");
        colors.add("Black");
        colors.add("Green");
 
        ListIterator listIterator
            = colors.listIterator();
 
        // Print the original ArrayList created
        System.out.println("List Before remove() :- "
                           + colors);
 
        // we want to remove Blue element from the arraylist
        for (String it : colors) {
            listIterator.next();
            // if we reched to required element break the
            // loop
            if (it == "Blue")
                break;
        }
 
        // remove color blue from arraylist
        listIterator.remove();
 
        System.out.println("List After remove():- "
                           + colors);
    }
}


输出
List Before remove() method = [Red, White, Blue, Pink, Black, Green]
List After remove() method =  [Red, White, Pink, Black, Green]

情况 2:如果要删除的元素已知

迭代器/遍历 ArrayList 并递增列表迭代器。如果我们到达所需的元素,则中断循环,否则我们到达结尾并且不会删除任何内容。

Java

// Java Program to Remove an element from ArrayList
// using ListIterator
 
// Importing ArrayList and ListIterator classes
// of java.util package
import java.util.ArrayList;
import java.util.ListIterator;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Create an ArrayList
        ArrayList colors = new ArrayList();
 
        // Adding elements to the arraylist
        colors.add("Red");
        colors.add("White");
        colors.add("Blue");
        colors.add("Pink");
        colors.add("Black");
        colors.add("Green");
 
        ListIterator listIterator
            = colors.listIterator();
 
        // Print the original ArrayList created
        System.out.println("List Before remove() :- "
                           + colors);
 
        // we want to remove Blue element from the arraylist
        for (String it : colors) {
            listIterator.next();
            // if we reched to required element break the
            // loop
            if (it == "Blue")
                break;
        }
 
        // remove color blue from arraylist
        listIterator.remove();
 
        System.out.println("List After remove():- "
                           + colors);
    }
}
输出
List Before remove() :- [Red, White, Blue, Pink, black, green]
List After remove():- [Red, White, Pink, black, green]