欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

ArrayList中的remove方法详解,arraylistremove

来源: javaer 分享于  点击 41036 次 点评:77

ArrayList中的remove方法详解,arraylistremove


ArrayList类中一共给出了两种remove方法,下面来对这两种方法详解:

1、参数为元素下标(int型)
对于这个方法,里面的参数是要删除元素的下标,我们可以使用students(arraylist定义的对象名).remove(students.indexOf(stu(类定义的对象名)))如下(只是示范,具体的自己写):

static List<Student> students = new ArrayList<Student>();
for(Student stu : students){
		if(stu.getSno().equals(sno)){
				students.remove(students.indexOf(stu));
				System.out.println("该数据已删除!");
				return;
		}
}

2、参数为对象名
对于这个方法,参数是你写的类定义的对象名,不过在前面应该加上(类名)来表明你所使用的类,不然会报错。如下:

static List<Student> students = new ArrayList<Student>();
for(Student stu : students){
		if(stu.getSno().equals(sno)){
				students.remove((Student)stu);
				System.out.println("该数据已删除!");
				return;
		}
}

以上就是我对remove方法的理解与详解,希望能帮到你们。

相关文章

    暂无相关文章

用户点评