Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的arrayList在删除时跳过一个元素_Java_Arraylist - Fatal编程技术网

Java 为什么我的arrayList在删除时跳过一个元素

Java 为什么我的arrayList在删除时跳过一个元素,java,arraylist,Java,Arraylist,我想删除数组的三个元素(索引0、1和2),然后将接下来的三个元素移到索引0、1、2中。由于某些原因,数组没有删除正确的元素 ArrayList <String> tryThis = new <String>ArrayList(); tryThis.add("first"); tryThis.add("second"); tryThis.add("third"); tryThis.add("fourth"

我想删除数组的三个元素(索引0、1和2),然后将接下来的三个元素移到索引0、1、2中。由于某些原因,数组没有删除正确的元素

       ArrayList <String> tryThis = new <String>ArrayList();
       tryThis.add("first");
       tryThis.add("second");
       tryThis.add("third");
       tryThis.add("fourth");
       tryThis.add("fifth");
       tryThis.add("sixth");
       tryThis.add("seventh");
       tryThis.add("eighth");
       tryThis.add("Ninth");

       System.out.println(tryThis.get(0) + tryThis.get(1) + tryThis.get(2));

       tryThis.remove(0);  
       tryThis.remove(1);
       tryThis.remove(2);

       System.out.println(tryThis.get(0) + tryThis.get(1) + tryThis.get(2));
ArrayList tryThis=new ArrayList();
tryThis.添加(“第一”);
tryThis.添加(“第二”);
tryThis.添加(“第三”);
tryThis.添加(“第四”);
tryThis.添加(“第五”);
tryThis.添加(“第六”);
tryThis.添加(“第七”);
tryThis.添加(“第八”);
tryThis.添加(“第九”);
System.out.println(tryThis.get(0)+tryThis.get(1)+tryThis.get(2));
删除(0);
t.移除(1);
t.移除(2);
System.out.println(tryThis.get(0)+tryThis.get(1)+tryThis.get(2));

我希望它在删除之前打印“第一、第二和第三”,然后在删除之后打印“第四、第五和第六”。但是,它会在移除后打印“第一个第二个第三个”,然后打印“第二个第四个第六个”。为什么?

删除索引0处的元素后,前一个索引1元素被移到索引0,依此类推。因此,应在索引0处删除三次:

tryThis.remove(0);  
tryThis.remove(0);
tryThis.remove(0);

remove(0)
方法通过删除列表的第一个(索引0)元素来更改列表,因此所有其他元素的索引也会更改。尝试调用
remove(0)
三次,以获得预期的结果。

您有两种可能

您可以运行以下代码3次(例如,使用for循环):

或者,以相反的方式运行代码:

tryThis.remove(2);  
tryThis.remove(1);
tryThis.remove(0);
请记住,使用
remove(int-index)
,它会删除此列表中指定位置的元素,并将任何后续元素向左移动(从其索引中减去一个)


注意:如前所述,您应该使用正确的语法:

ArrayList <String> tryThis = new ArrayList<String>();
ArrayList tryThis=new ArrayList();
如果类型是明显的(在您的案例和使用中是隐式的),您也可以将第二个
放在一边:

ArrayList <String> tryThis = new ArrayList<>();
ArrayList tryThis=new ArrayList();

删除时,按降序删除。 源代码: 删除此列表中指定位置的元素。 将任何后续元素向左移动(从其 指数)

publicstaticvoidmain(字符串[]args){
ArrayList tryThis=新的ArrayList();
tryThis.添加(“第一”);
tryThis.添加(“第二”);
tryThis.添加(“第三”);
tryThis.添加(“第四”);
tryThis.添加(“第五”);
tryThis.添加(“第六”);
tryThis.添加(“第七”);
tryThis.添加(“第八”);
tryThis.添加(“第九”);
System.out.println(tryThis.get(0)+tryThis.get(1)+tryThis.get(2));
int i[]={2,1,0};
对于(int j=0;j
只需执行
尝试此操作,删除(0)
tryThis,删除(0)
tryThis,删除(0)
第0个元素在每次移除后都会更改,但这不会解决任何问题,除非
ArrayList()
应该是
ArrayList()
移除第一个元素。拆下第二个元件。拆下第三个元件。它正按照您的要求进行操作。如果您经常删除元素,请不要使用ArrayList。此插图将有助于更好地理解。代码运行良好。初始元素:第一,第二,第三,第四,第五,第六,第七,第八,第九移除(0)元素:第二,第三,第四,第五,第六,第七,第八,第九移除(1)元素:第二,第四,第五,第六,第六,第七,第九移除(2)元素:第二,第四,第六,第七,第八,第九最后一秒,第四、第六、第七、第八、第九
ArrayList <String> tryThis = new ArrayList<>();
public static void main(String[] args) {
         ArrayList <String> tryThis = new <String>ArrayList();
           tryThis.add("first");
           tryThis.add("second");
           tryThis.add("third");
           tryThis.add("fourth");
           tryThis.add("fifth");
           tryThis.add("sixth");
           tryThis.add("seventh");
           tryThis.add("eighth");
           tryThis.add("Ninth");

           System.out.println(tryThis.get(0) + tryThis.get(1) + tryThis.get(2));

           int i[] = {2,1,0};

           for (int j = 0; j < i.length; j++) {
               tryThis.remove(i[j]);
           }

           System.out.println(tryThis.get(0) + tryThis.get(1) + tryThis.get(2));
    }