Java集合框架
Posted H+
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java集合框架相关的知识,希望对你有一定的参考价值。
Collection框架Demo(java.util.Collection)
1,Collection集合的基本Demo:
package com.hpioneer.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
/**
* @Description: ColLection转数组遍历
* @Author: HPioneer
* @CreateTime: 2018/5/7 17:40
* @File: Collection of JavaProject in com.hpioneer.collection
* @FullFileName: com.hpioneer.collection.Collection
* @Create By IntelliJ
* @Version: 1.0
*/
public class Demo_Collection {
public static void main(String[] args) {
Collection c = new ArrayList();
boolean b1 = c.add("abc");
boolean b2 = c.add(true); //自动装箱new Boolean(true);
boolean b3 = c.add(100);
boolean b4 = c.add(new Student("张三",23));
boolean b5 = c.add("abc");
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
System.out.println(c.toString());
Object[] arr = c.toArray();
for (int i =0 ;i<arr.length;i++){
System.out.println(arr[i]);
}
System.out.println(c.size());
System.out.println(c);
}
}
2,Collection迭代器遍历
package com.hpioneer.collection;
import sun.plugin.WJcovUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* @Description:
* @Author: HPioneer
* @CreateTime: 2018/5/7 17:51
* @File: Iterator of JavaProject in com.hpioneer.collection
* @FullFileName: com.hpioneer.collection.Iterator
* @Create By IntelliJ
* @Version: 1.0
*/
public class Demo_Iterator {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add(new Student("张三",23));
c.add(new Student("李四",25));
c.add(new Student("王五",25));
Iterator it = c.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.getName()+" "+s.getAge());
}
}
}
3,ListIterator
package com.hpioneer.collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.ListIterator;
/**
* @Description: ListIterator的add,遍历元素,集合修改元素
* @Author: HPioneer
* @CreateTime: 2018/5/7 17:40
* @File: Collection of JavaProject in com.hpioneer.collection
* @FullFileName: com.hpioneer.collection.Collection
* @Create By IntelliJ
* @Version: 1.0
*/
public class Demo_Collection {
public static void main(String[] args) {
List list = new ArrayList();
list.add("a"); //Object obj = new String();
list.add("b");
list.add("world");
list.add("c");
list.add("d");
list.add("e");
ListIterator lit = list.listIterator(); //获取迭代器(List集合特有的)
while(lit.hasNext()) {
String str = (String)lit.next(); //向下转型
if("world".equals(str)) {
//list.add("javaee"); //遍历的同时在增加元素,并发修改ConcurrentModificationException
lit.add("javaee");
}
}
System.out.println(list);
}
}
4,Vector的迭代
package com.hpioneer.collection;
import java.util.Enumeration;
import java.util.Vector;
/**
* @Description:
* @Author: HPioneer
* @CreateTime: 2018/5/7 18:14
* @File: Demo_Vector of JavaProject in com.hpioneer.collection
* @FullFileName: com.hpioneer.collection.Demo_Vector
* @Create By IntelliJ
* @Version: 1.0
*/
public class Demo_Vector {
public static void main(String[] args) {
Vector v = new Vector();
v.addElement("a");
v.addElement("b");
v.addElement("c");
v.addElement("d");
Enumeration en = v.elements(); //获取枚举
while(en.hasMoreElements()) { //判断集合中是否有元素
System.out.println(en.nextElement()); //获取集合中的元素
}
}
}
以上是关于Java集合框架的主要内容,如果未能解决你的问题,请参考以下文章