一、概念
java容器类类库的用途是“”保存对象“,并将其划分为两个不同的概念。
(1)Collection
其有三个子类:List,Queue,Set
List:必须按照顺序存放;
Queue:队列的特点存放;
Set:不能有重复的元素;
(2)Map
一组键值对
二、List
List有两种类型
(1)ArrayList,也就是数据结构中讲的顺序表类型,这种数据类型,插入和删除比较慢。查找起来比较容易。
(2)LinkedList,数据结构中的链表,插入和删除比较方便。查找起来比较慢。
三、迭代器
Iterate是一个对象,它的工作是遍历并选择序列中的对象,而客户端成员不必知道或者关心该序列底层的结构。
(1)使用next()方法获得序列中的下一个元素
(2)使用hasNext()检查序列中是否还有元素
(3)使用remove()将迭代器新近返回的元素删除
public class Arraylistdemo {
public static void main(String[] args)
{
Collection<Integer> c = new ArrayList<Integer>();
for(int i=0;i<=10;i++)
{
c.add(i);
}
Iterator<Integer> it = c.iterator();
while(it.hasNext())
{
Integer a = it.next();
System.out.println(":"+a);
}
}
}
四、栈
Stack类:后进先出的特性
public class stacktest {
public static void main(String[] args)
{
Stack<String> stack = new Stack<String>();
// String a = "hello world";
for(String s : "hello world sfa daf g".split(" "))
// {
stack.push(s);
// }
while(!stack.empty())
{
System.out.println(stack.pop());
}
}
}
五、Set
set不保存重复的元素。
六、map
键值对的形式