我们通常使用Collections.sort()方法来对一个简单的数据列表排序.但是当ArrayList是由自定义对象组成的,就需要使用comparable或者comparator接口了.在使用这两者进行排序之前,先尝试不实现任何接口来进行排序. 考虑下面的例子——有一个Student类,具有三个属性:name, rollno, age public class Student { private String name; private int rollno; private int age;
import java.util.ArrayList; import java.util.List; import java.util.Iterator; public class Study { public static void main(String[] args) throws Exception { List<String> list = new ArrayList<String>(); list.add("abcd"); list.add(&quo
Simple add() method is used for adding an element at the end of the list however there is another variant of add method which is used for adding an element to the specified index. public void add(int index, Object element) This method adds the elemen