import java.util.ArrayList; /** * Created by guipengfei on 2020/7/7 9:43 */ public class test1 { public static void main(String []args) { ArrayList<Integer> myList = new ArrayList<Integer>();//必须是Integer,不能是int for(int i=0;i<10;i++) myList.add(i);//向myList中加入对象参数 System.out.print("加入10个元素后: "); for(Integer elem:myList) { System.out.print(elem+" "); } System.out.println(); System.out.println("size="+myList.size());//获取myList中的元素个数 myList.remove(0);//删除索引为0的元素 myList.remove(Integer.valueOf(2));//删除元素2,必须转化成Integer,否则是索引删除 System.out.print("删除索引为0的元素和元素2后: "); for(Integer elem:myList) { System.out.print(elem+" "); } System.out.println(); System.out.println("删除以后的size="+myList.size());//获取myList中的元素个数 int index=myList.indexOf(3);//查找元素2对应的索引位置 System.out.println("元素3的index="+index); int e=myList.get(3);//获得索引位置3上面的元素 System.out.println("索引为3上面的元素是="+e); int b=9; if(myList.contains(b)) System.out.println("是否含有元素9?Yes"); } } /* add(Object elem) 向list中加入参数 remove(int index) 在索引参数中移除对象 remove(Object elem) 移除该对象 contains(Object elem) list含有该对象则返回true isEmpty() list中没有元素则返回true indexOf(Object elem) 返回对象的索引或-1 size() 返回list中的元素个数 get(int index) 返回当前索引参数的对象 */