day18.java

ArrayList类

它是一个引用 对象数组
案例:

package demo01;

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

public class Dem0o2Iterat0r {
    
    
	public static void main(String[] args) {
    
    
		Collection<String> coll= new ArrayList<>();
		coll.add("姚明"); 
		coll.add("科比");
		coll.add("麦迪");
		coll.add("布莱特");
		coll.add("詹姆斯");
		coll.add("奶粉也");
		Iterator<String> it = coll.iterator();
		if(it.hasNext()) {
    
    
			String e=it.next();
			System.out.println(e);
		}
		while(it.hasNext()) {
    
    
			String e=it.next();
			System.out.println(e);
		}
		for(Iterator<String> it2=coll.iterator();it.hasNext();) {
    
    
			String e=it.next();
			System.out.println(e);
		}
		System.out.println("=======================");
		//增强型for
		for(String s:coll) {
    
    
			System.out.println(s);
		}
		int [] arr1= {
    
    45,6,7,8,9};
		for(int i:arr1) {
    
    
			System.out.println(i);
		}
	}


}
结果
姚明
科比
麦迪
布莱特
詹姆斯
奶粉也
=======================
姚明
科比
麦迪
布莱特
詹姆斯
奶粉也
45
6
7
8
9

在这里插入图片描述在这里插入图片描述在这里插入图片描述

package demo01;

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

public class Demo01Collection {
    
    
	public static void main(String[] args) {
    
    
		
		//Collection<String> coll =new ArrayList<>();//体现了多态
		Collection<String> coll =new HashSet<>();//体现了多态
		System.out.println(coll);
		boolean b1=coll.add("张三");
		System.out.println(b1);
		System.out.println(coll);
		coll.add("李四");
		coll.add("李四");
		coll.add("王五");
		coll.add("赵六");
		System.out.println(coll);
		boolean b2=coll.remove("王五");
		System.out.println(b2);
		System.out.println(coll);
		boolean b3=coll.contains("赵六");
		System.out.println(b3);
		System.out.println(coll);
		boolean b4=coll.remove("王五");
		System.out.println(b4);
		System.out.println(coll);
		boolean b5=coll.isEmpty();
		System.out.println(b5);
		System.out.println(coll);
		int b6=coll.size();
		System.out.println(b6);
		System.out.println(coll);
		System.out.println("=================");
		Object[]  arr =coll.toArray();
		for(int i=0; i<arr.length;i++) {
    
    
			System.out.println(arr[0]);
		}
		coll.clear();
		System.out.println(coll);
		b5=coll.isEmpty();
		System.out.println(b5);
		
		
	}
	}
[]
true
[张三]
[李四, 张三, 王五, 赵六]
true
[李四, 张三, 赵六]
true
[李四, 张三, 赵六]
false
[李四, 张三, 赵六]
false
[李四, 张三, 赵六]
3
[李四, 张三, 赵六]
=================
李四
李四
李四
[]
true
	

编译期异常和运行期异常

package demo02;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class demo02Excption {
    
    
	
public static void main(String[] args) {
    
    
	//编译期异常
	SimpleDateFormat sdf =new  SimpleDateFormat("yyyy-mm-dd");
	Date date =null;
	System.out.println("第一句测试");
	try {
    
    
		date =sdf.parse("2021-05-11");
	} catch (ParseException e) {
    
    
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}
	System.out.println(date);
	
	
	//runtimeException  运行期异常
	int [] arr= {
    
    1,2,3};
	System.out.println(arr[0]);
	System.out.println(arr[2]);//有问题,但是不会提示,因为能够编译通过,在执行的时候才会有异常
	try {
    
    
		System.out.println(arr[2]);
	}catch(Exception e) {
    
    
		System.out.println("第一句测试");
		System.out.println(e);
		
	}
	//error 错误
	int [] arr2= new int[1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024*1024];
	System.out.println(arr2);
}
}
结果
第一句测试
Mon Jan 11 00:05:00 CST 2021
1
3
3
[I@728938a9

在这里插入图片描述在这里插入图片描述

20200806013

猜你喜欢

转载自blog.csdn.net/qq_55689246/article/details/116927230