JAVA萌新学习day13 包装类和字符串
一.包装类
1.数据的装箱与拆箱
装箱: 将基本类型数据包装成引用类型数据。
拆箱: 将包装类型数据转换成基本类型数据。
装箱和拆箱的方法:
//装箱:使用包装类中的构造方法,或静态valueOf方法
int a = 5;
Integer i = new Interger(a);
double b = 3.0;
Double d = Double.valueOf(b);
拆箱:
//拆箱:使用包装类中的xxVaule方法
Integer i = new Integer(5);
int a = i.intValue();
自动装箱和自动拆箱:
自动装箱:
//自动装箱:可以将基本类型数据赋值给包装类对象
int a = 5;
Integer i = a;//Integer.valueOf(a);
自动拆箱:
//自动拆箱:直接将包装类对象数据赋值给基本类型变量
Integer i = new Integer(5);
int a = i;//i.inValue();
Integer num1 = new Integer(100);
Integer num2 = new Integer(100);
System.out.println(num1 == num2);//false
Integer num3 = 100;//Integer.valueOf(100);
Integer num4 = 100;//Integer.valueOf(100);
System.out.println(num3 == num4);//true
2.基本类型与字符串之间的转换
2.1基本类型转字符串:
1)字符串链接:任何基本类型数据与字符串链接都变成字符串形式。
int a = 5;
//将基本类型变量与一个空字符串链接
String str = a + "";
2)String 类中的valueOf方法:
boolean boo = true;
String str = String.valueOf(boo);
2.2字符串转基本类型:
1)包装类中的parseXxx方法:
注意:
i.字符串不能直接接转成字符类型,需要使用String类中的charAt方法去字符串中取一个字符。
ii.若字符串转数值类型时,若字符串中存在不能表示数值的字符时,抛出java.lang.NumberFormatException异常。
iii.字符串转布尔类型时,当且仅当字符串是"true",否则其他任意字符串转布尔类型结果都是false
String str = "123";
int a = Integer.parseInt(str);
二.字符串以及包装类的应用
CustomClassStudy
package com.qf.java1904.day13;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CustomClassStudy {
//字符串的使用
public static void stringUse(){
System.out.println("-----1 获取字符串的长度:------");
//1 获取字符串的长度:
String con="我爱java,我爱学习,java是世界上最好的语言";
System.out.println(con.length());
//2获取某个字符或者字符串在原字符串中第一次出现的位置
System.out.println("-----2 字符或者字符串在原字符串中第一次出现的位置-------------");
int index=con.indexOf("java");
System.out.println(index);
int index2=con.indexOf("java", 7);
System.out.println(index2);
//3字符或者字符串在原字符串中最后一次出现的位置
System.out.println("-----3 字符或者字符串在原字符串中最后一次出现的位置-------------");
int index3=con.lastIndexOf("java");
System.out.println(index3);
//4获取某个位置上的字符
System.out.println("-----4获取某个位置上的字符-----------------");
char c=con.charAt(con.length()-1);
System.out.println(c);
System.out.println("-----5.判断字符串中是否包含某个子字符串----------");
boolean b=con.contains("php");
System.out.println("b:"+b);
System.out.println("-----6.判断字符串中是否相等-----------");
String str1="hello";
String str2=new String("hello");
System.out.println(str1.equals(str2));
System.out.println("");
System.out.println("-----7判断字符串中是否有内容----------------");
String str3=""; //空字符串 "" " "
System.out.println(str3.isEmpty());
System.out.println("-----8.判断字符串是否是以某个前缀开始的---------");
String name="张三丰";
System.out.println(name.startsWith("张三"));
System.out.println("-----9.判断字符串是否是以某个后缀结尾--------------");
String filename="HelloWorld.java";
System.out.println(filename.endsWith(".jpg"));
System.out.println("-----10.忽略大小写相等比较-----------------");
String add1="beijing";
String add2="BEIJING";
System.out.println(add1.equalsIgnoreCase(add2));
System.out.println("-------11 替换-----------");
con="我爱java,我爱学习,java是世界上最好的语言";
String con2=con.replace("java", "JAVA");
System.out.println(con2);
System.out.println("-------12截取------");
String sub=con.substring(7,11);//含头,不含结尾
System.out.println(sub);
System.out.println("-------13去除前面和尾部的空格---");
String city=" 北京市 昌平区 沙河 ";
System.out.println(city);
System.out.println(city.trim().replace(" ", ""));
System.out.println("-------14格式化字符串:将字符串按照指定的格式输出----");
System.out.println(String.format("%f", 3.141593));
System.out.println(String.format("%.2f", 3.141593));
System.out.println(String.format("%-10s", "hello")); //10个位置的长度 默认右对齐 -左对齐
System.out.println(String.format("%d", 100));
System.out.println(String.format("%X", 15));
System.out.println("--------15比较(字符串的大小位置)---------");
str1="移动";
str2="联通";
System.out.println(str1.compareTo(str2));
System.out.println("--------16拼接 相当于+号---------");
str3=str1.concat(str2);
System.out.println(str3);
System.out.println("--------17 拆分---------");
String s="java h5 ui php,python,c";
String[] languages=s.split("[ ,;]");//以空格、逗号和分号作为分隔符将字符串拆分
System.out.println("数组长度:"+languages.length);
for (String string : languages) {
System.out.println(string);
}
}
//包装类的使用
public static void wrapperUse(){
//1 装箱:把基本类型转成引用类型的过程
int num=10;
Integer integer1=new Integer(num);
Integer integer2=Integer.valueOf(num);
System.out.println(integer1==integer2);
//2拆箱:把引用类型转成基本类型
int num2=integer1.intValue();
int num3=integer2.intValue();
System.out.println("----------从jdk1.5 自动装箱和拆箱---------------");
int age=20;
//3自动装箱
Integer integer3=age;
System.out.println(integer3);
//4自动拆箱
int age2=integer3;
System.out.println(age2);
System.out.println("------------------其他包装类------------------");
double d=3.14;
Double double1=d;
double d2=double1;
boolean b=true;
Boolean boolean1=b;
boolean b2=boolean1;
System.out.println("-----------基本类型转字符串------------");
//基本类型转字符串
num=100;
//第一种方法
String s=num+"";
System.out.println(s);
//第二种方法
String s1=String.valueOf(num);
//第三种方法
String s2=Integer.toString(num);
//字符串转基本类型
System.out.println("-----------字符串转基本类型------------");
//第一种方法
age=Integer.parseInt("200");
//Float.parseFloat("1.5");
age2=Integer.parseInt("F", 16);//0-9 ABCDEF 10 11 12 13 14 15
int age3=Integer.parseInt("111",2);
System.out.println(age);
System.out.println(age2);
System.out.println(age3);
System.out.println("----------------Integer中其他方法------------------");
//把一个整数转成二进制字符串
String s3=Integer.toBinaryString(15);
//把一个整数转成16进制字符串
String s4=Integer.toHexString(15);
System.out.println(s3);
System.out.println(s4);
System.out.println("-----------boolean类和字符转换-------------------");
String s5="fajweifoawje";
b=Boolean.parseBoolean(s5);
System.out.println(b);
}
public static void stringBufferUse(){
//StringBuffer sb=new StringBuffer(); //随便
StringBuilder sb=new StringBuilder(); //随便
sb.append("hello");
System.out.println(sb.toString());
sb.append("world");
System.out.println(sb.toString());
sb.append("我爱java,我爱编程");
System.out.println(sb.toString());
//插入 insert
sb.insert(10, "魏志远");
System.out.println(sb.toString());
//替换 replace
sb.replace(15, 19, "meimv");
System.out.println(sb.toString());
//翻转 reverse
sb.reverse();
System.out.println(sb.toString());
//其他的方法
//删除
sb.delete(0, 10);
System.out.println(sb.toString());
//修改某个字符
sb.setCharAt(5, 'n');
System.out.println(sb.toString());
//清空
sb.delete(0, sb.length());
System.out.println(sb.toString());
}
/*
元字符
a 字符a
[abc] 匹配 a或b或c
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[ ^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
[a-z&&[ ^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
数量:
X? 一次或0次
X* 0次或多次(包括1次)
X+ 一次或多次
X{n} 恰好n次
X{n, } 至少n次
X{n,m} 至少n次,不超过m次
*/
public static void regexUse(){
// 匹配电话号码
String phoneNumber = "010-38389438";
boolean b = phoneNumber.matches("\\d{3,4}-\\d{7,8}");
System.out.println(b);
// 匹配手机号码
String phone = "13895271234";
System.out.println(phone.matches("[1][3-9]\\d{9}"));
// 匹配用户名:字母开头,数字字母下划线组合
String username = "abc1314";
System.out.println(username.matches("[a-zA-Z]+[\\w|_]*"));
// 匹配IP地址
String ip = "20.10.20.123";
System.out.println(ip.matches("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}"));
// 匹配网址
String addr = "http://www.baidu.com";
System.out.println(addr.matches("http://\\w+.\\w+.\\S*"));
// 匹配年龄
String age = "18";
System.out.println(age.matches("\\d{1,3}"));
// 匹配金额
String price = "19.8";
System.out.println(price.matches("\\d+.\\d+"));
}
public static void calendarUse(){
Calendar c = Calendar.getInstance();
System.out.println(c);
//打印出当前年份
System.out.println(c.get(Calendar.YEAR));
//设置年份
c.set(Calendar.YEAR, 2017);
System.out.println(c.get(Calendar.YEAR));
//读取指定路径下的文件
//文件路径根据自己的电脑上的路径设置
File file = new File("C:\\Users\\leisure\\IdeaProjects\\Java1904\\src\\com\\qf\\java1904\\day13\\CustomClassStudy.java");
//获得最后修改时间
long time = file.lastModified();
//将时间格式化
String ctime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(time));
System.out.println(ctime);
System.out.println(System.getProperties());
}
}
QFFile
package com.qf.java1904.day13;
/*
1.修改标准输出流,把日记输出都文件中
2. QFFile根据用户输入的根目录,读到它下面的所有内容到QFFile对象中
3.将目前QFFile对象中所有信息输出到文件中(按层次)
c:\a a b c d e.txt f.txt
d:\b a b c d e.txt f.txt
4.根据用户输入的目标目录,在它创建第一层目录下所有的内容
如何创建文件:
try{
File file = new File("d:\\d\\b.txt");
file.createNewFile();
}
catch(Exception e){
}
file.mkdirs();//创建多级文件夹
file.midir();//创建当前文件夹
*/
public class QFFile {
public static final int size = 1024;
//文件或文件夹的名字
private String name;
//保存当前文件夹下所有内容(如果当前对象是文件夹的情况下)
private QFFile[] files;
//当前对象是否是文件
private boolean isFile;
//当前对象是否是文件夹
private boolean isDirectory;
//当前所在的父目录(不包括自己)
private String homePath;
//用来记录当前数组中有效对象的个数
private int index;
public QFFile(){
this("",false,false,"c:\\temp");
}
public QFFile(String name, boolean isFile, boolean isDirectory, String homePath) {
index = 0;
this.name = name;
this.isFile = isFile;
this.isDirectory = isDirectory;
this.homePath = homePath;
//如果是文件夹,就创建数组
if(isDirectory){
//最大支持size 1024个元素
files = new QFFile[size];
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public QFFile[] getFiles() {
return files;
}
public void setFiles(QFFile[] files) {
this.files = files;
}
public boolean isFile() {
return isFile;
}
public void setFile(boolean file) {
isFile = file;
}
public boolean isDirectory() {
return isDirectory;
}
public void setDirectory(boolean directory) {
isDirectory = directory;
}
public String getHomePath() {
return homePath;
}
public void setHomePath(String homePath) {
this.homePath = homePath;
}
public void print (){
String info = isDirectory?"文件夹":"文件";
System.out.println("QFFile.print" + info + "名:" +name);
if(files == null){
System.out.println("空指针");
return;
}
//当前对象是文件夹
if(isDirectory){
System.out.println("包含的内容是:");
//遍历当前对象下的内容:即所有子文件和文件夹
for (int i = 0; i < files.length; i++) {
//获得到某一个子对象,可能是文件或文件夹
QFFile file = files[i];
//如果对象不为空,并且是文件夹
if(file != null && file.isDirectory){
//打印它的内容,递归
file.print();
}
if(file != null && file.isFile){
info = file.isDirectory?"文件夹":"文件";
System.out.println("QFFile.print" + info + "名:" + file.getName());
}
}
}
System.out.println("----------------------------------------------------");
}
public void addFile(QFFile file){
/*
1.当前对象:此方法作用就是给当前对象添加她的子文件或文件夹.
2.参数file,是当前对象的子对象(也就是说当前对象是文件夹,那么file对象就是它的子文件或文件夹)
2.因为当前对象的属性数组默认是1024大小,但里面都是空对象,所以需要有个索引记录真实有效对象的个数.
*/
//如果当前保存对象的子数组没有创建
if(files == null){
files = new QFFile[size];
}
//将当前对象保存到自己的数组中
files[index++] = file;
}
}
StudyDemo
package com.qf.java1904.day13;
import java.io.File;
import java.util.Random;
public class StudyDemo {
//递归:方法在自己的方法体内调用自己
//效果:相当于循环
//能用循环解决的不要用递归
//使用递归一定要给出终止条件,就是某种情况下不在调用自己
//读取指定目录下的结构
public static void readFile(String path){
//用传入的路径,构建一个File对象
File file = new File(path);
//Date date new File(path);
//StringBuffer buffer = new StringBuffer();
String name = file.getName();
String homePath = file.getPath();
boolean isFile = file.isFile();
boolean isDirectory = file.isDirectory();
QFFile qfFile = new QFFile(name,isFile,isDirectory,homePath);
readFile(file,0,qfFile);
qfFile.print();
}
public static void readFile(File file,int level,QFFile qfFile){
String str = "|";
for (int i = 0; i < level; i++) {
str += " -- ";
}
//判断当前对象是不是文件
if(file.isFile()){
//如果是文件,打印当前文件名
//System.out.println(str + "文件名:" + file.getName());
//如果是文件,保存文件的四个属性
qfFile.setName(file.getName());
qfFile.setHomePath(file.getPath());
qfFile.setDirectory(file.isDirectory());
qfFile.setFile(file.isFile());
return;
}
//获得当前目录下的所有文件和文件夹
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
File tempfile = files[i];
//QFFile newFile = new QFFile(tempfile.getName(),tempfile.isFile(),tempfile.isDirectory());
//如果当前对象是文件夹
if(tempfile.isDirectory()){
level++;
QFFile newFile = new QFFile();
//构造方法没有传参,我们的QFFile里的数组属性没有创建.
newFile.setName(tempfile.getName());
newFile.setHomePath(tempfile.getPath());
newFile.setDirectory(tempfile.isDirectory());
newFile.setFile(tempfile.isFile());
readFile(tempfile,level,newFile);
qfFile.addFile(newFile);
//QFFile[] subFiles = qfFile.getFiles();
//subFiles[index] = newFile;
}
else {
//打印当前文件名
//System.out.println(str + "文件名:" + tempfile.getName());
QFFile newFile = new QFFile();
//构造方法没有传参,我们的QFFile里的数组属性没有创建.
newFile.setName(tempfile.getName());
newFile.setHomePath(tempfile.getPath());
newFile.setDirectory(tempfile.isDirectory());
newFile.setFile(tempfile.isFile());
qfFile.addFile(newFile);
}
}
}
public static int factorial(int number, int level){ //5 4 3 2 1
System.out.println("level:" + level);
if(number == 1){
return 1;
}
else{
//level++;
return number * factorial(number-1,++level);
}
}
public static void main(String[] args) {
System.out.println("StudyDemo.main");
/*
int number = 5;
int value = factorial(number,1);
System.out.println(number + "的阶乘是" + value);
*/
readFile("E:\\Sublime Text 3");
}
//获得指定长度的随机字符
public static String randomString(int length){
String source = "abcdegfhijklmnopqrstuvwxyz";
Random random = new Random();
String result = "";
for (int i = 0; i < length; i++) {
int index = random.nextInt(source.length());
//从原字符串中获得指定索引位置的字符
result += source.charAt(index);
}
return result;
}
}