java 基础1

135 阅读10分钟
  • String
package cn.itcast.demo01;

public class StringTestDemo {
	public static void main(String[] args) {
		
		getLength();
		getSubString();
		
		
		String str = "fengfeng";
		boolean startsWith = str.startsWith("f");
		System.out.println(startsWith);
		
		boolean contains = str.contains("f");
		System.out.println(contains);
		
		int indexOf = str.indexOf("f");
		System.out.println(indexOf);
		
		byte[] bytes = "abc".getBytes();
		for(int i = 0; i < bytes.length;i++) {
			System.out.println(bytes[i]);
		}
		//System.out.println(bytes);
		
		char[] chars = "abc".toCharArray();
		System.out.println(chars[1]);
		
		//忽略大小写
		String s2 = "abc";
		String s3 = "AbC";
		boolean case1 = s2.equalsIgnoreCase(s3);
		System.out.println(case1);
		
		String s4 = "zhangyafeng";
		String substring = s4.substring(0, 1);
		String upper = substring.toUpperCase();
		String substring2 = s4.substring(1);
		String str_new = upper + substring2;
		
		System.out.println(s4.toUpperCase());
		System.out.println(str_new);
		
	}
	
	public static void getLength() {
		String s = "Fengfeng";
		System.out.println(s.length());
	}
	
	public static void getSubString() {
		String str = "fengfeng";
		String substring = str.substring(0, 2);
		System.out.println(substring);
	}
	
	
}
package cn.itcast.demo02;
/*
 *  实现正则规则和字符串进行匹配,使用到字符串类的方法
 *  String类三个和正则表达式相关的方法
 *    boolean matches(String 正则的规则)
 *    "abc".matches("[a]")  匹配成功返回true
 *    
 *    String[] split(String 正则的规则)
 *    "abc".split("a") 使用规则将字符串进行切割
 *     
 *    String replaceAll( String 正则规则,String 字符串)
 *    "abc0123".repalceAll("[\\d]","#")
 *    安装正则的规则,替换字符串
 */ 
public class StringBufferDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		StringBuffer s = new StringBuffer();
		s.append("fengfeng");
		s.append(1111);
		s.delete(0, 1);
		s.insert(0, "A");
		s.replace(0, 1, "C");
		s.reverse();
		//变为不可变的String
		s.toString();
		System.out.println(s);
		
		/*
		 * 检查QQ的合法性
		 * 0不能开头,全数字,位数5-10
		 * 
		 * 123456
		 * \d 表示 0-9
		 */
		String QQ = "123456";
//		boolean matches = QQ.matches("[1-9][0-9]{4,9}");
		boolean matches = QQ.matches("[1-9][\\d]{4,9}");
		System.out.println(matches);
		
		
	}

}
  • Date
package cn.itcast.demo03;

import java.util.Date;
/*
 *  时间和日期类
 *    java.util.Date
 *    
 *  毫秒概念: 1000毫秒=1秒
 *  
 *  毫秒的0点: 
 *     System.currentTimeMillis() 返回值long类型参数
 *     获取当前日期的毫秒值   3742769374405
 *     时间原点; 公元1970年1月1日,午夜0:00:00 英国格林威治  毫秒值就是0
 *     时间2088年8月8日
 *  
 *  重要: 时间和日期的计算,必须依赖毫秒值
 *    XXX-XXX-XX = 毫秒
 *    
 * 		long time = System.currentTimeMillis();
		System.out.println(time);
 */
public class DateDemo {
	public static void main(String[] args) {
		
		long time = System.currentTimeMillis();
		System.out.println(time);
		//
		Date date = new Date();
		System.out.println(date);
		
		//
		Date newdate = new Date(1000000);
		System.out.println(newdate);
		//时间转换成秒
		long l = date.getTime();
		System.out.println(l);
		//秒转换成时间
		date.setTime(2344556676778L);
		System.out.println(date);
		
		
		
	}
}
  • SimpleDateFormat(1)
package cn.itcast.demo02;

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

/*
 *  对日期进行格式化 (自定义)
 *    对日期格式化的类 java.text.DateFormat 抽象类, 普通方法,也有抽象的方法
 *    实际使用是子类 java.text.SimpleDateFormat 可以使用父类普通方法,重写了抽象方法
 */
public class SimpleDateFormatDemo {
	public static void main(String[] args) {
		function();
	}
	/*
	 * 如何对日期格式化
	 *  步骤:
	 *    1. 创建SimpleDateFormat对象
	 *       在类构造方法中,写入字符串的日期格式 (自己定义)
	 *    2. SimpleDateFormat调用方法format对日期进行格式化
	 *         String format(Date date) 传递日期对象,返回字符串
	 *    日期模式:
	 *       yyyy    年份
	 *       MM      月份
	 *       dd      月中的天数
	 *       HH       0-23小时
	 *       mm      小时中的分钟
	 *       ss      秒
	 *       yyyy年MM月dd日 HH点mm分钟ss秒  汉字修改,: -  字母表示的每个字段不可以随便写
	 */
	public static void function(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分钟ss秒");
		String date = sdf.format(new Date());
		System.out.println(date);
	}
}
  • SimpleDateFormat(2)
package cn.itcast.demo02;

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

/*
 *   DateFormat类方法 parse
 *   将字符串解析为日期对象
 *   Date parse(String s) 字符串变成日期对象
 *   String => Date parse
 *   Date => String format
 *   
 */
public class SimpleDateFormatDemo1 {
	public static void main(String[] args) throws Exception{
		function();
	}
	/*
	 *  将字符串转成Date对象
	 *  DateFormat类方法 parse
	 *  步骤:
	 *    1. 创建SimpleDateFormat的对象
	 *       构造方法中,指定日期模式
	 *    2. 子类对象,调用方法 parse 传递String,返回Date
	 *    
	 *    注意: 时间和日期的模式yyyy-MM-dd, 必须和字符串中的时间日期匹配
	 *                     1995-5-6
	 *    
	 *    但是,日期是用户键盘输入, 日期根本不能输入
	 *    用户选择的形式
	 */
	public static void function() throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse("1995-5-6");
		System.out.println(date);
	}
}
package cn.itcast.demo04;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class DateTest {
	public static void main(String[] args) throws Exception {
		function();
	}
	/*
	 *  闰年计算
	 *  2000 3000
	 *  高级的算法: 日历设置到指定年份的3月1日,add向前偏移1天,获取天数,29闰年
	 */
	public static void function_1(){
		Calendar c = Calendar.getInstance();
		//将日历,设置到指定年的3月1日
		c.set(2088, 2, 1);
		//日历add方法,向前偏移1天
		c.add(Calendar.DAY_OF_MONTH, -1);
		//get方法获取天数
		int day = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(day);
	}
	
	/*
	 *  计算活了多少天
	 *   生日  今天的日期
	 *   两个日期变成毫秒值,减法
	 */
	public static void function() throws Exception {
		System.out.println("请输入出生日期 格式 YYYY-MM-dd");
		//获取出生日期,键盘输入
		String birthdayString = new Scanner(System.in).next();
		//将字符串日期,转成Date对象
		//创建SimpleDateFormat对象,写日期模式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//调用方法parse,字符串转成日期对象
		Date birthdayDate = sdf.parse(birthdayString);
		
		//获取今天的日期对象
		Date todayDate = new Date();
		
		//将两个日期转成毫秒值,Date类的方法getTime
		long birthdaySecond = birthdayDate.getTime();
		long todaySecond = todayDate.getTime();
		long secone = todaySecond-birthdaySecond;
		
		if(secone < 0){
			System.out.println("还没出生呢");
		}
		else{
		System.out.println(secone/1000/60/60/24);
		}
		
	}
}

Integer

package cn.itcast.demo01;

public class IntegerDemo {
	public static void main(String[] args) {
		
		int i = Integer.parseInt("1");
		System.out.println(i);
		
		
		int i1 = 3;
		String s = i1 +"";
		System.out.println(s);
		
		
		String string = Integer.toString(3);
		System.out.println(string);
		
		
		Integer in = new Integer("100");
		int intValue = in.intValue();
		System.out.println(intValue);
		
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Integer.MIN_VALUE);
		
		Integer in1 = 1;
		System.out.println(in1);
		
	}
}	
/*
	 * Integer类的3个静态方法
	 * 做进制的转换
	 * 十进制转成二进制  toBinarString(int)
	 * 十进制转成八进制  toOctalString(int)
	 * 十进制转成十六进制  toHexString(int)
	 * 三个方法,返回值都是以String形式出现
	 */
	public static void function_1(){
		System.out.println(Integer.toBinaryString(99));
		System.out.println(Integer.toOctalString(99));
		System.out.println(Integer.toHexString(999));
	}
	
	/*
	 *   Integer类的静态成员变量
	 *   MAX_VALUE
	 *   MIN_VALUE
	 */
	public static void function(){
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Integer.MIN_VALUE);
	}
package cn.itcast.demo1;
/*
 *   JDK1.5后出现的特性,自动装箱和自动拆箱
 *   自动装箱: 基本数据类型,直接变成对象
 *   自动拆箱: 对象中的数据变回基本数据类型
 */
public class IntegerDemo2 {
	public static void main(String[] args) {
		function_2();
	}
	/*
	 *  关于自动装箱和拆箱一些题目
	 */
	public static void function_2(){
		Integer i = new Integer(1);
		Integer j = new Integer(1);
		System.out.println(i==j);// false 对象地址
		System.out.println(i.equals(j));// true  继承Object重写equals,比较的对象数据
		
		System.out.println("===================");
		
		Integer a = 500;
		Integer b = 500;
		System.out.println(a==b);//false
		System.out.println(a.equals(b));//true
		
		System.out.println("===================");
		
		
		//数据在byte范围内,JVM不会从新new对象
		Integer aa = 127; // Integer aa = new Integer(127)
		Integer bb = 127; // Integer bb = aa;
		System.out.println(aa==bb); //true
		System.out.println(aa.equals(bb));//true
	}
	
	
	//自动装箱和拆箱弊端,可能出现空指针异常
	public static void function_1(){
	    Integer in =null;	
	    //in = null.intValue()+1
	    in = in + 1;
	    System.out.println(in);
	}
	
	//自动装箱,拆箱的 好处: 基本类型和引用类直接运算
	public static void function(){
		//引用类型 , 引用变量一定指向对象
		//自动装箱, 基本数据类型1, 直接变成了对象
		
		Integer in = 1; // Integer in = new Integer(1)
		//in 是引用类型,不能和基本类型运算, 自动拆箱,引用类型in,转换基本类型
		
		//in+1  ==> in.inValue()+1 = 2    
		// in = 2    自动装箱
		in = in + 1;
		
		System.out.println(in);
		
	}
}
/*
    ArrayList<Integer> ar = new ArrayList<Integer>();
    ar. add(1);
 */

Math

package cn.itcast.demo3;
/*
*  数学计算的工具类
*  java.lang.Math 静态方法组成
*/
public class MathDemo {
   public static void main(String[] args) {
   	function_6();
   }
   /*
    *  static double round(doubl d)
    *  获取参数的四舍五入,取整数
    */
   public static void function_6(){
   	double d = Math.round(5.4195);
   	System.out.println(d);
   }
   
   /*
    *  static double random() 返回随机数 0.0-1.0之间
    *  来源,也是Random类
    */
   public static void function_5(){
   	for(int i = 0 ; i < 10 ;i++){
   		double d = Math.random();
   		System.out.println(d);
   	}
   }
   
   /*
    * static double sqrt(double d)
    * 返回参数的平方根
    */
   public static void function_4(){
   	double d = Math.sqrt(-2);
   	System.out.println(d);
   }
   
   /*0
    * static double pow(double a, double b)
    * a的b次方
    */
   public static void function_3(){
   	double d = Math.pow(2, 3);
   	System.out.println(d);
   }
   
   /*
    * static double floor(double d)
    * 返回小于或者等于参数d的最大整数
    */
   public static void function_2(){
   	double d = Math.floor(1.5);
   	System.out.println(d);
   }
   
   /*
    *  static double ceil(double d)
    *  返回大于或者等于参数d的最小整数
    */
   public static void function_1(){
   	double d = Math.ceil(5.1);
   	System.out.println(d);
   }
   
   /*
    *  static int abs(int i)
    *  获取参数的绝对值
    */
    public static void function(){
   	int i = Math.abs(0);
   	System.out.println(i);
    }
    
}

Arrays

package cn.itcast.demo4;

import java.util.Arrays;

/*
 *  数组的工具类,包含数组的操作
 *  java.util.Arrays
 */
public class ArraysDemo {
	public static void main(String[] args) {
		function_2();
		int[] arr = {56,65,11,98,57,43,16,18,100,200};
		int[] newArray = test(arr);
		System.out.println(Arrays.toString(newArray));
	}
	/*
	 *  定义方法,接收输入,存储的是10个人考试成绩
	 *  将最后三个人的成绩,存储到新的数组中,返回新的数组
	 */
	public static int[] test(int[] arr){
		//对数组排序
		Arrays.sort(arr);
		//将最后三个成绩存储到新的数组中
		int[] result = new int[3];
		//成绩数组的最后三个元素,复制到新数组中
	//	System.arraycopy(arr, 0, result, 0, 3);
		for(int i = 0 ;  i < 3 ;i++){
			result[i] = arr[i];
		}
		return result;
	}
	
	/*
	 *  static String toString(数组)
	 *  将数组变成字符串
	 */
	public static void function_2(){
		int[] arr = {5,1,4,6,8,9,0};
		String s = Arrays.toString(arr);
		System.out.println(s);
	}
	
	/*
	 *  static int binarySearch(数组, 被查找的元素)
	 *  数组的二分搜索法
	 *  返回元素在数组中出现的索引
	 *  元素不存在, 返回的是  (-插入点-1)
	 */
	public static void function_1(){
		int[] arr = {1,4,7,9,11,15,18};
	    int index =  Arrays.binarySearch(arr, 10);
	    System.out.println(index);
	}
	
	/*
	 *  static void sort(数组)
	 *  对数组升序排列
	 */
	public static void function(){
		int[] arr = {5,1,4,6,8,9,0};
		Arrays.sort(arr);
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}
}

BigInteger

package cn.itcast.demo5;

import java.math.BigInteger;

/*
 *  超级大的整数运算
 *    超过long取值范围整数,封装成BigInteger类型的对象
 */
public class BigIntegerDemo {
	public static void main(String[] args) {
		function_1();
	}
	/*
	 * BigInteger对象的四则运算
	 * 调用方法计算,计算结果也只能是BigInteger对象
	 */
	 public static void function_1(){
		 BigInteger b1 = new BigInteger("5665464516451051581613661405146");
		 BigInteger b2 = new BigInteger("965855861461465516451051581613661405146");
		 
		 //计算 b1+b2对象的和,调用方法 add
		 BigInteger bigAdd = b1.add(b2);//965855867126930032902103163227322810292
		 System.out.println(bigAdd);
		 
		 //计算b1-b2对象的差,调用方法subtract
		 BigInteger bigSub = b1.subtract(b2);
		 System.out.println(bigSub);
		 
		 //计算b1*b2对象的乘积,调用方法multiply
		 BigInteger bigMul = b1.multiply(b2);
		 System.out.println(bigMul);
		 
		 //计算b2/b1对象商,调用方法divied
		 BigInteger bigDiv = b2.divide(b1);
		 System.out.println(bigDiv);
	 }
	
	/*
	 * BigInteger类的构造方法
	 * 传递字符串,要求数字格式,没有长度限制
	 */
	public static void function(){
		BigInteger b = new BigInteger("8465846668464684562385634168451684568645684564564");
		System.out.println(b);
		BigInteger b1 = new BigInteger("5861694569514568465846668464684562385634168451684568645684564564");
		System.out.println(b1);
	}
}

BigDecimal

package cn.itcast.demo5;

import java.math.BigDecimal;

public class BigDecimalDemo {
	public static void main(String[] args) {
		function_1();
	}
	/*
	 * BigDecimal实现除法运算
	 * divide(BigDecimal divisor, int scale, int roundingMode) 
	 * int scale : 保留几位小数
	 * int roundingMode : 保留模式
	 * 保留模式 阅读API文档
	 *   static int ROUND_UP  向上+1
	 *   static int ROUND_DOWN 直接舍去
	 *   static int ROUND_HALF_UP  >= 0.5 向上+1
	 *   static int ROUND_HALF_DOWN   > 0.5 向上+1 ,否则直接舍去
	 */
	public static void function_1(){
		BigDecimal b1 = new BigDecimal("1.0301");
		BigDecimal b2 = new BigDecimal("100");
		//计算b1/b2的商,调用方法divied
		BigDecimal bigDiv = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//0.01301
		System.out.println(bigDiv);
	}
	
	/*
	 *  BigDecimal实现三则运算
	 *  + - *
	 */
	public static void function(){
		BigDecimal b1 =  new BigDecimal("0.09");
		BigDecimal b2 =  new BigDecimal("0.01");
		//计算b1+b2的和,调用方法add
		BigDecimal bigAdd = b1.add(b2);
		System.out.println(bigAdd);
		
		BigDecimal b3 = new BigDecimal("1");
		BigDecimal b4 = new BigDecimal("0.32");
		//计算b3-b2的差,调用方法subtract
		BigDecimal bigSub = b3.subtract(b4);
		System.out.println(bigSub);
		
		BigDecimal b5 = new BigDecimal("1.015");
		BigDecimal b6 = new BigDecimal("100");
		//计算b5*b6的成绩,调用方法 multiply
		BigDecimal bigMul = b5.multiply(b6);
		System.out.println(bigMul);
	}
}


/*
 * 计算结果,未知
 * 原因: 计算机二进制中,表示浮点数不精确造成
 * 超级大型的浮点数据,提供高精度的浮点运算, BigDecimal
System.out.println(0.09 + 0.01);//0.09999999999999999
System.out.println(1.0 - 0.32);//0.6799999999999999
System.out.println(1.015 * 100);//101.49999999999999
System.out.println(1.301 / 100);//0.013009999999999999 
*/

Iterator

package cn.itcast.demo;

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

/*
 *  集合中的迭代器:
 *    获取集合中元素方式
 *  接口 Iterator : 两个抽象方法
 *     boolean hasNext() 判断集合中还有没有可以被取出的元素,如果有返回true
 *     next() 取出集合中的下一个元素
 *     
 *  Iterator接口,找实现类.
 *    Collection接口定义方法 
 *       Iterator  iterator()
 *    ArrayList 重写方法 iterator(),返回了Iterator接口的实现类的对象
 *    使用ArrayList集合的对象
 *     Iterator it = array.iterator(),运行结果就是Iterator接口的实现类的对象
 *     it是接口的实现类对象,调用方法 hasNext 和 next 集合元素迭代
 */
public class IteratorDemo {
	public static void main(String[] args) {
		Collection<String> coll = new ArrayList<String>();
		coll.add("abc1");
		coll.add("abc2");
		coll.add("abc3");
		coll.add("abc4");
		//迭代器,对集合ArrayList中的元素进行取出
		
		//调用集合的方法iterator()获取出,Iterator接口的实现类的对象
		Iterator<String> it = coll.iterator();
		//接口实现类对象,调用方法hasNext()判断集合中是否有元素
		//boolean b = it.hasNext();
		//System.out.println(b);
		//接口的实现类对象,调用方法next()取出集合中的元素
		//String s = it.next();
		//System.out.println(s);
		
		//迭代是反复内容,使用循环实现,循环的条件,集合中没元素, hasNext()返回了false
		while(it.hasNext()){
			String s = it.next();
			System.out.println(s);
		}
		
		/*for (Iterator<String> it2 = coll.iterator(); it2.hasNext();  ) {
			System.out.println(it2.next());
		}*/
		
	}
}