Curtis_yang
2017-12-09 13:13:46浏览 7027
package com.curtis.list;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TestList {
private List coursesToSelect;
public TestList() {
this.coursesToSelect = new ArrayList();
}
public void addMessage() {
Course c1 = new Course("1", "Java");
this.coursesToSelect.add(c1);
}
public int getListSize() {
return this.coursesToSelect.size();
}
public void getMessageFromListFor() {
for (int i = 0; i < this.coursesToSelect.size(); i++) {
Course course = (Course) this.coursesToSelect.get(i);
System.out.println(course.toString());
}
}
public void addMessageIndex() {
Course c2 = new Course("2", "C");
this.coursesToSelect.add(0, c2);
}
public void getMessageFromListIterator() {
Iterator iterator = this.coursesToSelect.iterator();
while (iterator.hasNext()) {
Course course = (Course) iterator.next();
System.out.println(course.toString());
}
}
public void getMessageFromListForeach() {
for (Object object : this.coursesToSelect) {
Course course = (Course) object;
System.out.println(course.toString());
}
}
public void getListForIndex() {
System.out.println(coursesToSelect.get(0));
}
public void removeMessageFromList() {
this.coursesToSelect.remove(0);
System.out.println(this.coursesToSelect.size());
}
public static void main(String[] args) {
TestList testList = new TestList();
testList.addMessage();
testList.addMessageIndex();
System.out.println(testList.getListSize());
System.out.println("==================================");
testList.getMessageFromListForeach();
System.out.println("==================================");
testList.getListForIndex();
System.out.println("==================================");
System.out.println(testList.getListSize());
testList.removeMessageFromList();
System.out.println("==================================");
}
}
热门评论
14、方法的调用,必须要实例化对象;在类中是能够存在实例化该类的对象的
12、泛型的引出
13、如果放入集合中的元素在放入前没有指明数据类型,那么再取出时是什么类型
1、使用集合导入包有哪些
2、add 方法
往集合中添加内容
3、get 方法
根据索引取集合中某个索引位置的元素
4、size 方法
集合长度
5、接口的实现类 List list=new ArrayList() ArrayList 是List 接口的实现类
6、iterator 方法
7、集合索引是从0开始
8、删除集合中的元素 remove 方法
9、集合遍历的三种方式
10、构造函数中的初始化
11、方法的返回值输出 getListSize()