集合(容器)

容器:以学生对象为例一个班级的每个学生都是一个对象,这些学生所在的班级就是容器集合作为容器

什么时候用到集合?

答:并不知道程序运行时需要多少对象,或者需要更复杂方式存储对象

Java的集合框架使用方便的接口和类,这些都位于Java.util包


List和Set继承了Collection接口,ArrayListshi

Collection 接口存储一组不唯一,无序的对象
List 接口存储一组不唯一,有序(插入顺序)的对象:不是进行了排序而是进入一个给一个编序号
Set 接口存储一组唯一,无序的对象 




Collection接口特有的方法



List接口特有下面的方法:凡是可以操作索引的方法都是该体系特有方法



可以使用Iterator遍历的本质是什么?实现Iterable接口

For-each循环
增强的for循环,遍历array或Collection的时候相当简便
无需获得集合和数组的长度,无需使用索引访问元素,无需循环条件
遍历集合时底层调用Iterator完成操作

For-each缺

数组:不能方便的访问下标值      不要在for-each中尝试对变量赋值,只是一个临时变量
集合:与使用Iterator相比,不能方便 的删除集合中的内容


For-each总结:除了简单的遍历并读出其中的内容外,不建议使用增强for

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

public class Demo {
	public static void main(String[] args){
		Collection s=new ArrayList();
		s.isEmpty();//判断容器是否为空,返回布尔值
		s.add("abc");//向容器中添加元素
		s.remove("abc")//只能删除存在得对象  原型为remove(Object obj)
	}
}
上述代码是向容器中添加内容add函数的原型是 add(Object obj);


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

public class bianli {
	public static void main(String[] args) {
		Collection<String> c1=new ArrayList<String>();
		c1.add("java");
		c1.add("html");
		c1.add("sql");
		//1.集合的进行遍历    C ollection接口继承了 Iterable(迭代器) 加强for循环 (1.5版本以上的)才可以用
		for(Object o:c1){//左边面是输出的对象,右边是输入对象
			System.out.println(o);
		}
		for(String s:c1){//s就是迭代变量  c1是迭代对象就是集合名称   String就是集合中元素定义的数据类型
			System.out.println(s);
		}
		//第二种:使用迭代方法来完成集合的遍历  Iterator
		//首先
		Iterator<String> it=c1.iterator();//不要去new,就能得到迭代气对象
		while(it.hasNext()){//用来判断是否有迭代元素
			Object o=it.next();//可以得到迭代器中的元素(可以将集合中的元素取出),next+1;
			System.out.println(o);
		}
		//Iterator it1=c1.iterator()初始化变量
		//it.hasNext()判断条件
		//it1.next();循环增量
		for(Iterator<String> it1=c1.iterator();it1.hasNext();){
			System.out.println(it1.next());
		}
	}
}


import java.util.ArrayList;
import java.util.List;
/**
 * (CRUD)
 * 增   add("java")  、addAll(2, l2)
 * 删  remove(1) 、
 * 改  set(0, "SQL");
 * 查  get(1) 、subList(1, 3)
 * index  ???
 * @author admin
 *	作业:实现自定义对象的(student) 增、删、改、查功能。
 */
public class ArrayListDemo {

	public static void main(String[] args) {
		List<String> l1 = new ArrayList<String>();
		l1.add("java");
		l1.add("123");
		
		System.out.println(l1.size());
		//1.在指定位置上添加一个元素add(int arg0, Object arg1);    0 位置    ( 0  1 2)  3 java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
		l1.add(2, "abc");  //add(int arg0, Object arg1);   (arg0 >= 0   and arg0<=size)
		//2.在指定位置上添加多个元素addAll(int arg0, Collection arg1);
		List<String> l2 = new ArrayList<>();
		l2.add("html");
		l2.add("css");
		l2.add("hadoop");
		l2.add("andrand");
		l1.addAll(2, l2);
		
		//3.删除指定位置上的元素
		//l2.remove(1);  // 0 (包含0)到size-1
		//System.out.println(l2);
		
		//4.获取指定位置上的元素
		String s = l2.get(1);
		System.out.println(s);
		
		//5.修改指定元素  替换
//		System.out.println(l2);
//		l2.set(0, "SQL");
//		System.out.println(l2);
		//6.截取子集合   subList(int fromIndex, toIndex)  含头不含尾
		List l3 = l2.subList(1, 3);     
		System.out.println(l3);
		
		//遍历集合
//		for(String str : l1){
//			System.out.println(str);
//		}
	}

}



import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class CollectionDemo1 {

	public static void main(String[] args) {

		//1.list集合add()
//		Collection col1= new ArrayList(); //多态  实现类的向上转型
//		System.out.println("添加元素前,集合对象为空:"+col1.isEmpty()); 
//		String str="abc";
//		col1.add(str);
//		col1.add("abc");
//		Object o= new Integer(123456);  //自动装箱(1.5后)
//		Integer i= new Integer(123);  //自动装箱(1.5后)
//		col1.add(o);
//		col1.add(i);
//		col1.add("java");
		2.List集合的isEmpty() 、size()
//		System.out.println("添加元素后,集合对象不为空:"+col1.isEmpty());
//		System.out.println(col1);
//		System.out.println(col1.size());
		if(col1.remove(i)) System.out.println(o+"删除成功:");;
//		//col1.remove("abc");  //删除无效
//		System.out.println(col1.size());
//		System.out.println(col1);
//		4.list对象创建
//		List<Integer> l1 = new ArrayList<Integer>();
//		LinkedList<Object> linkedList = new LinkedList<Object>();
//		3.addAll(Collection c);  添加集合对象到另外的集合对象中
		Collection<String> c2 = new ArrayList<String>();
		c2.add("java");
		c2.add("html");
		c2.add("javascript");
		c2.add("css");
		c2.add("database");
//		col1.addAll(c2);
		System.out.println("c2->"+c2.toString());  //默认方法,被重写过
		
		List c1 =new ArrayList<>();
		c1.add("java");
		c1.add("abc");
		c1.add("123");
		System.out.println(c2);
//		c1.addAll(c2);
		System.out.println("c1->"+c1);
//		5.删除  remove()
//		System.out.println(col1.remove("java"));
//		System.out.println(col1.toString());
		//一次性删除全部
//		col1.clear();  
//		System.out.println(col1.toString());
		//将集合中相同的元素删除
//		System.out.println("调用removeAll()"+c1.removeAll(c2));
//		System.out.println(c1);
//		将集合中不相交的元素删除
//		c1.retainAll(c2);
		System.out.println(c1);
//		6.判断指定对象在集合中是否存在(查找对象),*****
		System.out.println(c1.contains("java000"));
//		7.比较集合中的元素是否相同
		List c3= new ArrayList<>();
		c3.add("java...");
		c3.add("abc");
		c3.add("123");
		System.out.println(c3);
		System.out.println(c1.equals(c3));
		c1.addAll(c3);
		System.out.println(c1);
		c1.retainAll(c3);
		System.out.println(c1);
		
		
		
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值