简体   繁体   English

如何使用 Arraylist 中的命令行同时删除两个对象

[英]How to remove two object at the same time using command line from an Arraylist

I have one list now.我现在有一份清单。 I want to remove two object when I used command line.当我使用命令行时,我想删除两个对象。

 Ex)
    javac Main.java
    
    java Main 2 3 
 a v i

I try changed code .I can not do that.我尝试更改代码。我不能那样做。 Please help me.请帮我。

Condition健康)状况

using Arraylist使用数组列表

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String[] s = {"a", "v", "c", "e", "i"};
        List<String> list = new ArrayList<>(s.length);
        for (String string : s) {
            list.add(string);
        }
        try {
                int index = Integer.parseInt(args[0]);
                
                list.remove(index);
                for (String string : list) 
                System.out.println(string);
        } catch(ArrayIndexOutOfBoundsException e){
            System.out.println("it is not good");
        }catch (IllegalArgumentException e){
            System.out.println("it is not good");
        }catch (IndexOutOfBoundsException e){
            System.out.println("it is not good");
        }
    }
}

Try the following code.试试下面的代码。 Your code only takes ONE argument args[0].您的代码只需要一个参数 args[0]。 Instead I used a for loop to iterate all given arguments.相反,我使用了一个 for 循环来迭代所有给定的参数。 However, this will not work in the way you are thinking, because the index is shifted when an item is removed.但是,这不会按照您的想法工作,因为删除项目时索引会移动。

public static void main(String[] args) {
        String[] s = {"a", "v", "c", "e", "i"};
        List<String> list = new ArrayList<>(s.length);
        for (String string : s) {
            list.add(string);
        }
        try {
            for (int i = 0; i< args.length; i++){
                int index = Integer.parseInt(args[i]);
                list.remove(index);
            }
            
            for (String string : list)
                System.out.println(string);
        } catch(ArrayIndexOutOfBoundsException e){
            System.out.println("it is not good");
        }catch (IllegalArgumentException e){
            System.out.println("it is not good");
        }catch (IndexOutOfBoundsException e){
            System.out.println("it is not good");
        }
    }

Instead, if you use the following, it adjusts the indexes from the arguments provided based on the number of items removed.相反,如果您使用以下内容,它会根据删除的项目数调整提供的参数中的索引。

for (int i = 0; i< args.length; i++){
                int index = Integer.parseInt(args[i])-i;
                list.remove(index);
            }

I just realised the code does not account for indexes that are not sorted.我刚刚意识到代码不考虑未排序的索引。 Thus, I added some code that will perform the sorting for you.因此,我添加了一些将为您执行排序的代码。 Use the following code instead.请改用以下代码。

int[] numbers = Arrays.stream(args).mapToInt(Integer::parseInt).toArray(); //convert args to int array
System.out.println(Arrays.toString(numbers));  //shows your arguments
Arrays.sort(numbers);  //sort your indexes in ascending order
System.out.println(Arrays.toString(numbers));  //shows sorted arguments

for (int i = 0; i< numbers.length; i++){
    list.remove(numbers[i] - i);
}

for (String string : list)
    System.out.println(string);

note, if you dont want to use numbers[i]-i you could instead iterate the array starting from the back of the array and use numbers[i] instead请注意,如果您不想使用numbers[i]-i您可以改为从数组的后面开始迭代数组并使用 numbers[i]

for (int i = numbers.length-1; i>=0; i--){
       list.remove(numbers[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM