Java学习笔记(四)
程序员文章站
2022-05-22 13:02:05
...
异常机制
常见异常分类
异常处理
捕获try-catch 声明异常(throws)
手动抛出异常 throw
public class TestThrow {
public static void main(String[] args) {
Person p=new Person();
p.setAge(-10);
}
}
class Person{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
if(age<=0) {
throw new IllegalAgeException("年龄不能为负数");
}
this.age = age;
}
}
class IllegalAgeException extends RuntimeException{
public IllegalAgeException() {
}
public IllegalAgeException(String msg) {
super(msg);
}
}
容器 collection
数组就是一种容器,可以在其中放置对象或基本数据类型,线性序列,快速访问数组元素,效率高,但是不灵活,容量事先定义好
Set没有顺序不可以重复 List有顺序可以重复 Map:HashMap
泛型
帮助我们建立安全的集合,泛型的本质就是“数据类型的参数化”,可以理解为数据类型的一个占位符(形式参数),即告诉编译器,在调用泛型时必须传入实际类型
package cn.chenye.collection;
public class TestGeneric {
public static void main(String[] args) {
MyCollection<String> mc=new MyCollection<String>();
mc.set("chenye", 0);
//mc.set(12313, 1);
//Integer a =(Integer)mc.get(1);
String b=mc.get(0);
System.out.print(b);
}
}
class MyCollection<E>{
Object[] objs=new Object[5];
public void set(E obj,int index) {
objs[index]=obj;
}
public E get(int index) {
return (E)objs[index];
}
}
List
有序,可重复的容器,List中每个元素都有索引标记,根据元素的索引标记访问元素,List允许加入重复的元素(e1.equals(e2))
List接口常用的实现类有3个,ArrayList,LinkedList,Vector
ArrayList底层是数组实现的,查询效率高,增删效率低,线程不安全,数组长度有限,但ArrayList是可以寻访任意数量的对象,长度不受限制
手动实现ArrayList
package cn.my.collection;
/**
* 自定义实现ArrayList
* @author ASUS
*
*/
public class MyArrayList {
private Object[] elementData;
private int size;
private static final int SIZE=10;
public MyArrayList() {
elementData=new Object[SIZE];
}
public MyArrayList(int capacity) {
elementData=new Object[capacity];
}
public static void main(String[] args) {
MyArrayList s1=new MyArrayList(20);
s1.add("asad");
s1.add("asdf");
System.out.println(s1);
}
public void add(Object obj) {
elementData[size++]=obj;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("[");
for(int i=0;i<size;i++) {
sb.append(elementData[i]+",");
}
sb.append("]");
return sb.toString();
}
}
ArrayList和LinkedList
LinkedList相当于链表
package cn.my.collection;
//import org.w3c.dom.Node;
/**
* 自定义链表
* @author ASUS
*增加小的封装,增加泛型
*/
public class MyLinkedList {
private Node first;
private Node last;
private int size;
public void add(int index ,Object obj) {
checkRange(index);
Node newNode=new Node(obj);
Node temp=getNode(index);
if(temp!=null) {
Node up=temp.previous;
up.next=newNode;
newNode.previous=up;
newNode.next=temp;
temp.previous=newNode;
}
}
public void remove(int index) {
checkRange(index);
Node temp=getNode(index);
if(temp!=null) {
Node up=temp.previous;
Node down=temp.next;
if(up!=null) {
up.next=down;
}
if(down!=null) {
down.previous=up;
}
if(index==0) {
first=down;
}
if(index==size-1) {
last=up;
}
size--;
}
}
public Object get(int index) {
checkRange(index);
Node temp=getNode(index);
return temp!=null?temp.element:null;
}
private void checkRange(int index) {
if(index<0||index>size-1) {
throw new RuntimeException("illegal!"+index);
}
}
public Node getNode(int index) {
checkRange(index);
Node temp=null;
if(index<=(size>>1)) {
temp=first;
for(int i=0;i<index;i++) {
temp=temp.next;
}
}
else {
temp=last;
for(int i=size-1;i>index;i--) {
temp=temp.previous;
}
}
return temp;
}
public void add(Object obj) {
Node node=new Node(obj);
if(first==null) {
first=node;
last=node;
}else {
node.previous=last;
node.next=null;
last.next=node;
last=node;
}
size++;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder("[");
Node temp=first;
while(temp!=null) {
sb.append(temp.element+" ,");
temp=temp.next;
}
sb.setCharAt(sb.length()-1, ']');
return sb.toString();
}
public static void main(String[] args) {
MyLinkedList lk=new MyLinkedList();
lk.add("c");
lk.add("h");
lk.add("e");
lk.add("n");
lk.add("y");
lk.add("e");
System.out.println(lk);
lk.add(4,"!");
System.out.println(lk);
}
}
class Node{
Node previous;
Node next;
Object element;
public Node(Node previous,Node next,Object obj) {
super();
this.previous=previous;
this.next=next;
this.element=element;
}
public Node(Object element) {
// TODO Auto-generated constructor stub
super();
this.element=element;
}
}
Vector向量 线程安全的
Map接口
Map是用来存储键值对的,key-value。Map类中存储的键值对通过键来标识,所以键值对不能重复
HashMap
- 哈希表的基本结构就是“数组+链表”,哈希表将两者优点结合 ***
- 基本结构 普通存储键值对
- 解决键重复的问题
- 重写toString方法 打印Map内容
- get查找键值对
- 完善封装 增加泛型
哈希表的底层实现原理:
- https://blog.csdn.net/zhaozao5757/article/details/79500475
- https://blog.csdn.net/ddfdjffd/article/details/96487143
package cn.chenye.collection;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
public static void main(String[] args) {
Map<Integer,String >m1=new HashMap<>();
m1.put(1, "chen");
m1.put(2, "ye");
m1.put(3, "!");
System.out.println(m1.get(1));
System.out.println(m1.size());
System.out.println(m1.isEmpty());
System.out.println(m1.containsKey(2));
System.out.println(m1.containsValue("!"));
Map<Integer,String >m2=new HashMap<>();
m2.put(4, "handsome");
System.out.println(m1.equals(m2));
m1.putAll(m2);
System.out.println(m1);
System.out.println(m2);
}
}
TreeMap
TreeMap是红黑二叉树的典型实现
Comparable接口 HashTable特点
HashMap线程不安全,效率高,允许key或value为null;
HashTable线程安全,效率低,不允许key或value为null;