集合的体系结构

 * 集合的体系结构:
 *         由于不同的数据结构(数据的组织方式,存储方式),所以Java为我们提供了不同的集合。
 *         但是不同集合功能相似,不断地向上提取,将共性抽取出来,这就是集合体系结构形成的原因

 * 体系结构:
 *         怎么学习?最顶层开始学习,因为最顶层包含了所有的共性
 *         怎么使用?使用最底层,因为最底层是具体的实现

 * collection:Collection 层次结构 中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。
 * 一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。

 * collection
 *     ||子类
 * List(列表)----有序、允许重复、有索引
 *     ||子类
 * ArrayList

 * 1、boolean add(E e) 
 * 2、void clear()  
 * 3、boolean contains(Object o)
 * 4、boolean isEmpty() 
 * 5、boolean remove(Object o)    
 * 6、int size()   
 * 7、Object[] toArray() 
 * 8、Iterator<E> iterator() 

import java.util.ArrayList;
import java.util.Collection;

/*
 * 集合的体系结构:
 * 1、boolean add(E e) 
 * 2、void clear()  
 * 3、boolean contains(Object o)
 * 4、boolean isEmpty() 
 * 5、boolean remove(Object o)    
 * 6、int size()   
 * 7、Object[] toArray() 
 * 8、Iterator<E> iterator() 
 * 
 * 
 * collection
 *  ||子类
 * List(列表)----有序、允许重复、有索引
 * 	||子类
 * ArrayList
 * 
 */
public class CollectionDemo {
	public static void main(String[] args) {
		//method01();
		//创建集合对象
		//Collection c = new Collection();Collection是接口,不能实例化。可以用他的子类
		Collection c = new ArrayList();//多态父类的引用指向子类

		//boolean add(E e):永远都可以添加成功,因为ArrayList他允许重复
		System.out.println(c.add("hello"));
		System.out.println(c.add("word"));
		
		//void clear() :清空集合
		//c.clear();
		
		//boolean contains(Object o):判断是否包含指定元素
		//System.out.println(c.contains("hello"));
		
		//boolean isEmpty():判断是否为空
		//System.out.println(c.isEmpty());
		
		//boolean remove(Object o):判断是否删除元素
		System.out.println(c.remove("java"));
		
		//Object[] toArray() :将集合转换成Object类型的数组
		Object[] obj = c.toArray();
		for (int i = 0; i < obj.length; i++) {
			System.out.print(obj[i]);
		}
		
		
		System.out.println(c.toString());
	}
	public static void method01() {
		//创建集合对象
		ArrayList a1 = new ArrayList();
		//添加元素
		a1.add("hello");
		a1.add("word");
		a1.add("java");
		//遍历结合
		for (int i = 0; i < a1.size(); i++) {
			System.out.print(a1.get(i));
		}
	}
}

 * 集合的遍历:

 * 集合的遍历方式:
 *         1、toArray():将集合转换成数组,然后遍历数组
 *         2、iterator(),可以返回一个迭代器对像,我们可以通过迭代器对象来迭代集合

 * Iterator:可以遍历集合
 *      E next() 
 *       返回迭代的下一个元素。 
 *       
 *   boolean hasNext() 
 *       如果仍有元素可以迭代,则返回 true。 
 *   注意:Exception in thread "main" java.util.NoSuchElementException
 *       使用Next()方法获取下一个元素,如果没有元素获取则出现NoSuchElementException

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/*
 * 集合的遍历方式:
 * 		1、toArray():将集合转换成数组,然后遍历数组
 * 		2、iterator(),可以返回一个迭代器对像,我们可以通过迭代器对象来迭代集合
 * Iterator:可以遍历集合
 * 	 E next() 
 *       返回迭代的下一个元素。 
 *       
 *   boolean hasNext() 
 *       如果仍有元素可以迭代,则返回 true。 
 *   注意:Exception in thread "main" java.util.NoSuchElementException
 *       使用Next()方法获取下一个元素,如果没有元素获取则出现NoSuchElementException
 */
public class IteratorDemo {

	public static void main(String[] args) {
		//method();
		//创建集合对象
		Collection c = new ArrayList();
		//添加元素
		c.add("hello");
		c.add("word");
		c.add("java");
		//获取迭代器对象
		Iterator it = c.iterator();
		//Object obj = it.next();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
	}

	public static void method() {
		//创建集合对象
		Collection c = new ArrayList();
		//添加元素
		c.add("hello");
		c.add("word");
		c.add("java");
		//获取数组
		Object[] obj = c.toArray();
		//遍历数组
		for (int i = 0; i < obj.length; i++) {
			System.out.println(obj[i]);
		}
	}

}

并发:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/*
 * 需求:
 * 		判断集合中是否包含Java,如果有则添加元素android
 * 
 * Exception in thread "main" java.util.ConcurrentModificationException
 * (使用迭代器去修改集合)并发修改异常
 * 迭代器是依赖集合的,相当于集合的副本,当迭代器在操作的时候,如果发现本身和集合不一样,则抛出异常
 * 
 * 解决方法:
 * 		在使用迭代器遍历的时候使用迭代器去修改集合
 */
public class IteratorDemo02 {

	public static void main(String[] args) {
		//method();
		//创建集合对象
		//Collection c = new ArrayList();
		List c = new ArrayList();
		//添加元素
		c.add("hello");
		c.add("word");
		c.add("java");
		/*//获取集合中的每一个元素然后进行比较
		Iterator it = c.iterator();
		while(it.hasNext()) {
			String str  = (String)it.next();
			if (str.equals("java")) {
				c.add("android");
			} 
		}*/
		
		ListIterator lit = c.listIterator();
		while(lit.hasNext()) {
			String str = (String)lit.next();
			if(str.equals("java")) {
				lit.add("android");
			}
		}
		System.out.println(c);
	}

	public static void method() {
		//创建集合对象
		Collection c = new ArrayList();
		//添加元素
		c.add("hello");
		c.add("word");
		c.add("java");
		//判断集合中是否包含元素Java
		if (c.contains("java")) {
			c.add("andriod");
		}
		System.out.println(c);
	}

}

 * (使用迭代器去修改集合)并发修改异常
 * 迭代器是依赖集合的,相当于集合的副本,当迭代器在操作的时候,如果发现本身和集合不一样,则抛出异常
 * 
 * 解决方法:
 *         在使用迭代器遍历的时候使用迭代器去修改集合

猜你喜欢

转载自blog.csdn.net/weixin_42698882/article/details/82598133