Java实训第十四天8/14

来源:互联网 发布:java工程师培训学费 编辑:程序博客网 时间:2024/05/23 01:24
*1.String 
String name="abc";
name=name+"ef";
2.StringBuffer
3.StringBuilder
append();
StringBuilder str=new StringBuilder();
str.append(123);
str.append("abc")
//123abc
toString();//转换为字符串
*4.正则表达式:比较重要,正则表达式是处理字符串的最优选择
[]
{}
a)格式验证:
boolean flag=Pattern.matches(regex,str);
b)查找数据并替换
String str="a1bbc2dddaf3";
把符合某种格式的数据替换
String demo=str.replaceAll("[a-zA-Z][0-9]","");
demo=str.replaceAll("([a-zA-Z])([0-9])","$2$1");
c)按照某种格式分割数据
split(regex)
split(regex,limit)

5.网络扒虫
a)网络编程
b)正则表达式 ,编写或去数据的格式
c)java代码使用正则表达式获取数据
d)jdbc
e)多线程
添加IP地址的限定
6.包装类
Integer int
Character char

Integer i=null;
int j=i;//错误  i.intValue();
String str="";
int a=Integer.parseInt(str);
7.Date 
getYear();距离1900年过了多少年
getMonth();返回的月份比实际小 1 
getDay():
Calendar c=Calendar.getInstance();
//无为都是获取一个时间
时间的用法:java.util.Date;
Date date=new Date();
SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str=f.format(date);
8.Math
double i=Math.ceil(n): 大于等于n的最小整数 x.0
floor()
round() :对小数部分四舍五入,只保留整数部分
记住Math类的特点:
final修饰
所有属性方法都是static修饰
构造方法私有,不能创建对象
=================================================
集合框架
1.集合就是我们这生活中用于存放各种事物的容器
2.集合中 存放的都是 对象,自动装箱
List list=new ArrayList();
list.add("aaa");
list.add(123);//Integer.valueOf(123);
3.集合的容量,可以无限期增大,可以自动扩容
4.数组和集合
a)数组:长度固定,存放数据的类型单一,可以存放基本类型和引用类型
访问比较快
b)集合:可以自动扩容,只要是对象都可以存放,只能放引用类型
访问比数组要慢
5.什么情况使用数组和集合
a)数组用在数据类型确定,数据固定的情况
b)集合用在数据个数不确定的情况
使用集合存放从数据库中读取的数据
select * from user_info;  //100
select * from user_Info where u_age>20;
6.线性集合:一个挨着一个依次存放,中间不能出现空白,是连续
a)List:有序的可以重复
ArrayList:基于数组的列表,其实就是把数组包装了以下
LinkedList:基于的是链表的列表
Vector:给java 中gui JTable 绑定数据的格式
b)Set:无序,不能重复
HashSet:以数据的哈希码存放
TreeSet:按照升序排列
7.ArrayList:
a)使用数组存放数据:
每次扩容容量的一半
int i=n/2;  
int j=n>>1;
从0 开始第一次 10 
b)泛型:规范集合中存放数据的类型
泛型 只能是包装类或引用类型
List<Integer> list=new ArrayList<Integer>();
1.7后,尽量少用 
List<Integer> list=new ArrayList<>();
i)作用:
1)在添加数据时检查数据的类型,避免数据添加错误
2)在获取数据时减少 强制类型转换
遍历:
a)for循环
b)加强的for循环 for :
for(数据类型 变量:数组或集合){

}

作业:
1.使用ArrayList 装载十个成绩,随机生成,要求保留两位小数
获取其中的最大成绩
和平均成绩
Collections
Arrays
=========================================
1.ArrayList 
a)可变长度的数组
b)有序,其中的数据可以重复
c)常用的方法
i)add(Object obj):在最后 添加要给数据
 add(int index,Object obj):在执行index位置上插入一个数据
index 必须 <= size()
ii)size():返回集合中元素的个数
iii)get(int index):获取下标为 index的数据
d)如何遍历一个ArrayList集合
*i)普通for循环,用于没有泛型的集合,需要显示下标
List list=new ArrayList();
for(int i=0;i<list.size();i++){
Object obj=list.get(i);
System.out.println(i+":"+obj);
}
*ii)加强的for循环:只针对于泛型集合,或者不需要考虑下标的情况
List<String> list=new ArrayList<String>();
for(String str:list){

}
iii)利用迭代器:转换了一种数据读取方式
使用 Iterator 迭代其中数据
boolean flag=it.hasNext() :判断是否有下一个数据
Object obj=it.next() :获取下一个值

List list=new ArrayList();
......
Iterator it=list.iterator();
while(it.hasNext()){
Object obj=it.next();
}

iv)1.8以上 
List<String> list=new ArrayList<String>();
list.forEach(str->{System.out.println(str);});


2.LinkedList:基于链表的存储方式
ArrayList和LinkedList的区别:
a)基于存储数据的容器不同
ArrayList:数组
LinkedList:链表
b)方法不同
LinkedList 比 ArrayList 多个 6个方法可以直接操作首尾的元素
注意:如果要使用该 6 个特有的方法 
不能使用 List list=new LinkedList() ,
因为 父类或父接口引用执行子类或实现类对象
只能调用父类或接口中声明的方法,子类特有的无法访问
只能使用 LinkedList link=new LinkedList();
c)效率不同:
ArrayList 随机访问效率高
LinkedList 添加删除是效率高



3.Set:无序,不能重复
a)Set集合是以Map为存储容器的,添加到set集合中的数据
自动添加到Map的key中
b)遍历集合:
i)先转为数组,再通过下标遍历
Set<String> set=new HashSet<String>();
1)直接转换为Object数组
Object[] objs=set.toArray();
2)使用泛型
//定义泛型数组的格式
String[] ns=new String[0];
ns=set.toArray(ns);
ii)直接使用加强的for循环就可以遍历 set
Set<String> set=new HashSet<String>();

for(String str:set){

}
iii)依然可以使用Iterator
4.现有字符串数组如下:
String[] names={"aaa","ccc","aaa","bbb","ccc"};
去除数组中重复的内容
集合:
List:
Set 

Set<String> set=new HashSet<String>();
for(String str:names){
set.add(str);
}

ii)不用循环,直接把数组转换为集合,再把集合添加到Set中























int i=12;
Integer n=i;
int[] nums={};
//Integer[] ms=nums;

异常的层次结构
Throwable
Exception Error
RuntimeExceptionIOException
运行时异常 编译时异常
非检查时异常 检查时异常
uncheckedExceptioncheckedException
NullPointerExceptionFileNotFoundException
ArraysIndexOutOfBoundsExceptionClassNotFoundException













原创粉丝点击