Collection集合和常用功能
Collection集合
常用方法
booLean add(E e); 向集合中添加元素
boolean remove(E e); 删除集合中的某个元素
void clear( ); 清空集合所有的元素
boolean contains(E e); 判断集合中是否包含某个元素
boolean isEmpty( ); 判断集合是否为空
int size(); 获取集合的长度
object[] toArray(); 将集合转成一个数组
public static void main(String[] args) {
//创建集合对象C
Collection<String> coll = new ArrayList<String>();// booLean add(E e);
//向集合中添加元素
coll.add( "hello");
coll.add( "wor1d");
coll.add( "heima" ) ;
coll.add( "java" ) ;
System.out.println(coll);//hello,worLd, heima,java]
// //boolean remove(E e);
// //删除集合中的某个元素
// boolean result = coll.remove ( "helLo" );
// System.out.println( result);
// System.out.println(coll);
// // void clear();
// //清空集合所有的元素
// coll.clear( );
// System.out.println(coll);
//
//// boolean contains(E e);
// boolean result1 = coll.contains("java");
// System.out.println(result1);
// boolean isEmpty();
System.out.println(coll.isEmpty());
//int size();
//获取集合的长度
//
System.out.println(coll.size() );
//object[ ] toArray ();
//将集合转成一个数组
Object[] arr = coll.toArray();
//遍历数组
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
Collection集合常用功能
java.util.collection接口
所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法任意的单列集合都可以使用collection接口中的方法
共性的方法:
public boolean add(E e):把给定的对象添加到当前集合中。
public void clear():清空集合中所有的元素。
public boolean remove(E e):把给定的对象在当前集合中删除。
public boolean contains(E e):判断当前集合中是否包含给定的对象。
public boolean isEmpty():判断当前集合是否为空。
public int size():返回集合中元素的个数。
public 0bject[] toArray ():把集合中的元素,存储到数组中。
public static void main(String[] args) { //创建集合对象,可以使用多态 Collection<String> coll = new ArrayList<String>(); System.out.println(coll);//重写了tostring方法[] //public boolean add(E e):把给定的对象添加到当前集合中。返回值是一个boolean值,一般都返回true,所以可以不用接收 boolean b1 = coll.add("张三"); System.out.println( "b1 : "+b1);//b1 :truesystem.out.println(coll);//[张三〕 coll.add("李四"); coll.add("赵六"); coll.add("田七"); System.out.println(coll);//[张三,李四,赵六,田七] //public boolean remove(E e):把给定的对象在当前集合中删除。返回值是一个boolean值,集合中存在元素,删除元素,返回true //集合中不存在元素,删除失败,返回false boolean b2 = coll.remove("赵六"); System.out.println( "b2 : "+b2);//b2 :true boolean b3 = coll.remove("赵四"); System.out.println( "b3: "+b3); System.out.println( coll);//[张三,李四,田七] //public boolean contains(E e):判断当前集合中是否包含给定的对象。包含返回true //不包含返回false*/ boolean b4 = coll.contains("李四"); System.out.println( "b4 : "+b4);//b4 :true boolean b5 = coll.contains("赵四"); System.out.println( "b5 : "+b5);//b5 :false //public boolean isEmpty():判断当前集合是否为空。集合为空返回true,集合不为空返回false boolean b6 = coll.isEmpty() ; System.out.println( "b6 : "+b6);//b6:false // public int size():返回集合中元素的个数。 int size = coll.size(); System.out.println( "size: "+size); //public object[] toArray();//把集合中的元素,存储到数组中。 Object[]arr = coll.toArray(); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } //public void clear():清空集合中所有的元素。但是不删除集合,集合还存在 coll.clear(); System.out.println(coll); System.out.println(coll.isEmpty()); }
public static void main(String[] args) { //创建集合对象,可以使用多态 Collection<String> coll = new ArrayList<>(); System.out.println(coll);//重写了toString方法[] /* public boolean add(E e):把给定的对象添加到当前集合中。 返回值是一个Boolean值,一般都返回true,所以可以不用接受。 */ boolean b1 = coll.add("1"); System.out.println("b1:"+b1);//true System.out.println(coll);//[张三] coll.add("2"); coll.add("3"); coll.add("4"); coll.add("5"); coll.add("6"); coll.add("7"); coll.add("8"); System.out.println(coll);//[1, 2, 3, 4, 5, 6, 7, 8] /* public boolean remove(E e):把给定的对象在当前集合中删除。 返回值是一个Boolean,集合中存在元素,删除元素,返回true 集合中不存在元素,删除失败,返回false */ boolean b2 = coll.remove("3"); System.out.println("b2:"+b2);//b2:true boolean b3 = coll.remove("9"); System.out.println("b3:"+b3);//b3:false System.out.println(coll);//[1, 2, 4, 5, 6, 7, 8] /* public boolean contains(E e):判断当前集合中是否包含给定的对象。 包含返回true 不包含返回false */ boolean b4 = coll.contains("4"); System.out.println("b4:"+b4);//b4:true boolean b5 = coll.contains("10"); System.out.println("b5:"+b5);//b5:false /* public boolean isEmpty():判断当前集合是否为空,集合为空返回true,集合不为空返回false */ boolean b6 = coll.isEmpty(); System.out.println(b6);//false /* public int size():返回集合中元素的个数。 */ int size = coll.size(); System.out.println("size:"+size);//size:7 /* public Object[] toArry():把集合中的元素,存储到数组中。 */ Object[] arr = coll.toArray(); for (int i = 0; i < arr.length; i++) { if(i == arr.length-1){ System.out.println(arr[i]); }else { System.out.print(arr[i]+" ");//1 2 4 5 6 7 8 } } /* public void clear():清空集合中所有的元素,但是不删除集合,集合还存在 */ coll.clear(); System.out.println(coll);//[] System.out.println("是否为空"+coll.isEmpty());//是否为空:true System.out.println("size:"+coll.size());//size:0 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix