A:将集合转换成数组,通过遍历数组的方式实现集合遍历
a:存储字符串并遍历
Collection c = new ArrayList();
c.add("java");
c.add("hello");
c.add("world");
Object[] objs = c.toArray();
for(int x= 0;x < objs.length; x++){
String s = (String)objs[x];
}
b:存储自定义对象并遍历
Collection c = new ArrayList();
Student s1 = new Student("杨过",25);
Student s2 = new Student("郭靖",26);
Student s3 = new Student("乔峰",27);
c.add(s1);
c.add(s2);
c.add(s3);
Object[] objs = c.toArray();
for(int x = 0;x < objs.length; x++){
Student s = (Student)objs[x];
String name = s.getName();
int age = s.getAge();
}
B:使用迭代器遍历
a:存储字符串并遍历
Collection c = new ArrayList();
c.add("java");
c.add("hello");
c.add("world");
Iterator it = c.iterator();
while(it.hasNext()){
String s = (String)it.next();
}
b:存储自定义对象并遍历
Collection c = new ArrayList();
Student s1 = new Student("杨过",25);
Student s2 = new Student("郭靖",26);
Student s3 = new Student();
s3.setName("段誉");
s3.setAge(23);
c.add(s1);
c.add(s2);
c.add(s3);
c.add(new Student("乔峰",30));
Iterator it = c.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
String name = s.getName();
int age = s.getAge();
}
2.List
A:存储字符串并使用普通for循环(size()和get()方法结合)实现遍历
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
for(int x = 0;x < list.size(); x++){
String s = (String)list.get(x);
}
B:存储自定义对象并使用普通for循环(size()和get()方法结合)实现遍历
List list = new ArrayList();
Student s1 = new Student("杨过",25);
Student s2 = new Student("郭靖",26);
Student s3 = new Student("乔峰",27);
list.add(s1);
list.add(s2);
list.add(s3);
for(int x = 0;x < list.size(); x++){
Student s = (Student)list.get(x);
String name = s.getName();
int age = s.getAge()
}
C:并发修改异常及处理方式
a:并发修改异常演示
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
//迭代器遍历
Iterator it = list.iterator();
while(it.hasNext()){
String s = (String)it.next();
if("world".equals(s)){
list.add("javaSE"); //此处发生并发修改异常
}
}
c:处理方式二:使用普通for循环
for(int x = 0;x < list.size(); ++){
String s = (String)list.get(x);
if("world".equals(s)){
list.add("javaSE");
}
}
3.ArrayList
A:去除字符串重复元素
a:创建新集合保存不重复元素
ArrayList array = new ArrayList();
array.add("hello");
array.add("world");
array.add("hello");
ArrrayList newArray = new ArrayList();
Iterator it = array.iterator();
while(it.hasNext()){
String s = (String)it.next();
if(!newArray.contains(s)){
newArray.add(s);
}
}
b:元素依次比较删除后边的重复元素
for(int x = 0;x < array.size() - 1;x++){
for(int y = x + 1;y < array.size();y++){
if(array.get(x).equals(array.get(y))){
array.remove(y);
y--;
}
}
}
B:去除自定义对象重复元素
重写hashCode()和equals()方法
ArrayList array = new ArrayList();
创建对象添加元素
ArrayList newArray = new ArrayList();
Iterator it = array.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
if(!newArrray.contains(s)){
newArray.add(s)
}
}
4.LinkedList
A:用linkedList模拟一个栈数据结构的集合类并测试
public class MyStack{
private LinkedList link;
public MyStack(){
link = new LinkedList();
}
public void add(Object obj){
link.addFirst(obj);
}
public Object get(){
return link.removeFirst();
}
public boolean isEmpty(){
return link.isEmpty();
}
}
public class MyStackDemo{
public static void main(String[] args){
MyStack ms = new Mystack();
ms.add("hello");
ms.add("world");
ms.add("java");
while(!ms.isEmpty){
System.out.println(ms.get());
}
}
}
5.list集合综合案例
A:集合的嵌套遍历
//创建大集合
ArrayList<ArrayList<Student>> bigArray = new ArrayList<ArrayList<Student>>();
//创建小集合1
ArrayList<Student> array1 = new ArrayList<Student>();
创建自定义对象并添加为集合元素
//创建小集合2
ArrayList<Student> array2 = new ArrayList<Student>();
创建自定义对象并添加为集合元素
//创建小集合3
ArrayList<Student> array3 = new ArrayList<Student>();
创建自定义对象并添加为集合元素
//将小集合添加为大集合元素
bigArray.add(array1);
bigArray.add(array2);
bigArray.add(array3);
//遍历集合
for(ArrayList<Student> array : bigArray){
for(Student s : array){
String s = s.getName();
int s = s.getAge();
}
}
B:获取10个1-20之间的随机数,要求不能重复
Random r = new Random();
ArrayList<Integer> array = new ArrayList<Integer>();
int count = 0;
while(count < 10){
int number = r.nextInt(20) + 1;
if(array.contains(number)){
array.add(number);
count++;
}
}
for(Integer i : array){
System.out.println(i);
}
C:键盘录入多个数据,以0结束,输出这个数据中的最大值
Scanner sc = new Scanner(System.in);
ArrayList<Integer> array = new ArrayList<Integer>();
while(true){
int number = sc.nextInt();
if(number != 0){
array.add(number);
}else{
break;
}
}
Integer[] ints = new Integer[array.size()];
array.toArray(ints);
Arrays.sort(ints);
int max = ints[ints.length - 1];
6.HashSet
A:获取10个1-20的随机数,要求不能重复
Random r = new Random();
HashSet<Integer> hs = new HashSet<Integer>();
while(hs.size() < 10){
int num = r.nextInt(20) + 1;
hs.add(num);
}
for(Integer i : hs){
System.out.println(i);
}
7.TreeSet
A:自然排序
//按照姓名长度排序
public class Student inplements Comparable<Student>{
private String name;
private int age;
...
public int compareTo(Student s){
int num = this.name.length() - s.name.length();
int num2 = num == 0? this.name.compareTo(s.name): num;
int num3 = num2 == 0? this.age - s.age : num2;
return num3;
}
}
B:比较器排序
//按照姓名长度排序
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){
public int compare(Student s1,Stuent s2){
int num = s1.getName().length() - s2.getName().length();
int num2 = num == 0? s1.getName().compareTo(s2.getName()) : num;
int num3 = num2 == 0? s1.getAge() - s2.getAge() : num2;
return num3;
}
});
8.Map
A:Map集合的遍历
Map<String,String > map = new HashMap<String,String>();
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
map.put("张无忌","赵敏");
方式一:
Set<String> set = map.keySet();
for(String key : set){
String value = map.get(key);
}
方式二:
Set<Map.Entry<String,String>> set = map.entrySet();
for(Map.Entry<String,String> me : set){
String key = me.getKey();
String value = me.getValue();
}
B:嵌套集合遍历
9.TreeSet
A:获取一个字符串中每个字符出现的次数
字符串:"aababcabcdabcde" 结果:a(5)b(4)c(3)d(2)e(1)
String s = "aababcabcdabcde";
char[] chs = s.toCharArray();
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
for(char ch : chs){
Integer i = tm.get(ch);
if(i == null){
tm.put(ch,1);
}else{
i++;
tm.put(ch,i);
}
}
StringBuilder sbu = new StringBuilder();
Set<Character> set = tm.keySet();
for(Character key : set){
Integer value = tm.get(key);
sbu.append(key).append("(").append(value).append(")");
}
String result = sbu.toString();