黑马程序员—常见API+Arrays工具类+System类+StringBuffer类+基本数据包装类+Date类+DateFormat类+Calendar类

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

API

 

一 Arrays 工具类的使用Arrays 是针对数组操作的工具类。

成员方法:

*public static String toString(int[] a):把整型数组转变成字符串。

*public static void sort(int[] a):对数组进行排序。

*public static int binarySearch(int[] a,int key):对数组进行二分查找。 —> 前提:数组必须是有序的。

 

二 System 类的使用:

1System:System 类包含一些有用的类字段和方法。它不能被实例化。 提供了静态的变量和方法供我们使用。

   (2)成员方法:

public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。 

  public static long currentTimeMillis():返回以毫秒为单位的当前时间。

 

三 StringBuffer 类的使用:

 (1StringBuffer:字符串缓冲区类。

 (2String的区别?

String一旦被赋值,值不能发生改变。而StringBuffer,值还可以改变。

      为什么呢?

  StringBuffer采用的是缓冲区机制。

一开始,首先开辟一些空间,然后,随着数据的最多,然后,还可以继续开辟空间。这些操作针对的是同一个对象。

 (3)构造方法:

StringBuffer() 

StringBuffer(int capacity) 

StringBuffer(String str) 

 

***String和StringBuffer的转换:通过构造方法可以实现。

 

 (4)成员方法:

   *public int length():字符个数。实际长度。

   *public int capacity():字符容量。理论长度。

 

     A 添加功能:

public StringBuffer append(int i):在末尾追加元素。

  public StringBuffer insert(int index,int i):在指定位置添加元素。

 

    B 删除功能:

StringBuffer deleteCharAt(int index):删除指定位置字符。

StringBuffer delete(int start, int end):删除指定开始位置和结束位置间的字符 。

 

 C 替换功能:

StringBuffer replace(int start, int end, String str):把开始到结束位置的字符用一个新的字符串给替换。 

 
 D 截取功能:

String substring(int start):从指定位置到末尾截取。

  String substring(int start, int end): 从指定位置到结束位置截取。

 

 E 字符串反转:

StringBuffer reverse():字符串反转。

 

 

补充:

冒泡排序,二分查找

 

1 StringBuilder效率比StringBuffer高。

 

2 StringBuilder是线程不安全的,StringBuffer是线程安全的。

 

 

integer i = 5;如果等号右边的数字是在byte范围之内的话,那么就从byte常量池中直接拿出来,赋给i。(享元设计模式)

 

static修饰的API类名.方法名直接调用。

 

代码示例:

/*

StringBuffer是字符串缓冲区。

 

是一个容器。

特点:

1,长度是可变化的。

2,可以字节操作多个数据类型。

3,最终会通过toString方法变成字符串。

 

 

 

C create U update R read D delete

 

1,存储。

StringBuffer append():将指定数据作为参数添加到已有数据结尾处。

StringBuffer insert(index,数据):可以将数据插入到指定index位置。

 

 

2,删除。

StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end

StringBuffer deleteCharAt(index):删除指定位置的字符。

3,获取。

char charAt(int index) 

int indexOf(String str) 

int lastIndexOf(String str) 

int length() 

String substring(int start, int end) 

 

4,修改。

StringBuffer replace(start,end,string);

void setCharAt(int index, char ch) ;

 

 

5,反转。

StringBuffer reverse();

 

6

将缓冲区中指定数据存储到指定字符数组中。

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 

 

JDK1.5 版本之后出现了StringBuilder.

 

StringBuffer是线程同步。

StringBuilder是线程不同步。

 

以后开发,建议使用StringBuilder

 

升级三个因素:

1,提高效率。

2,简化书写。

3,提高安全性。

 

*/

class Demo
{
}
 
class StringBufferDemo 
{
public static void main(String[] args) 
{
//method_update();
 
StringBuilder sb = new StringBuilder("abcdef");
char[] chs = new char[6];
 
 
sb.getChars(1,4,chs,1);//将
 
for(int x=0; x<chs.length; x++)
{
sop("chs["+x+"]="+chs[x]+";");
}
 
draw(3,6);
draw(8,9);
 
//	StringBuilder sb1 = new StringBuilder();
//	sb1.append(new Demo()).append(new Demo());
//	sop("sb1="+sb1);
}
public static void method_update()
{
StringBuffer sb  = new StringBuffer("abcde");
 
//	sb.replace(1,4,"java");
sb.setCharAt(2,'k');
 
 
sop(sb.toString());
}
public static void method_del()
{
StringBuffer sb  = new StringBuffer("abcde");
//	sb.delete(1,3);
//清空缓冲区。
//sb.delete(0,sb.length());
 
//sb.delete(2,3);
sb.deleteCharAt(2);
 
sop(sb.toString());
}
 
public static void method_add()
{
StringBuffer sb = new StringBuffer();
 
 
//sb.append("abc").append(true).append(34);
//	StringBuffer sb1 = sb.append(34);
//	sop("sb==sb1:"+(sb==sb1));
 
sb.insert(1,"qq");
sop(sb.toString());//abctrue34
//sop(sb1.toString());
 
}
 
public static void sop(String str)
{
System.out.println(str);
}
public static void draw(int row,int col)
{
StringBuilder sb = new StringBuilder();
for(int x=0; x<row; x++)
{
for(int y=0; y<col; y++)
{
sb.append("*");
}
sb.append("\r\n");
}
 
sop(sb.toString());
}
 
}


 

 

 

四.基本类型包装类(掌握)

1.基本类型的数据我们只能使用值,不能做更多的操作。为了方便我们操作,java就把每种基本类型进行了包装。提供方法供我们使用。

 

     2.基本类型和包装类的对应关系

基本类型 包装类

 * byte Byte

 * short Short

 * int  Integer

 * long Long

 * float Float

 * double Double

 * char Character

 * boolean Boolean

 

   3.Integer构造方法

A:Integer i = new Integer(int num);

B:Integer i = new Integer(String s);

注意:s必须是一个由数字字符组成的字符串。

 

   4.Stringint类型的转换

A:String -- int

Integer:

public static int parseInt(String s)

B:int -- String

Integer:

public static String toString(int i)

String:

public static String valueOf(int i)

 

   5.JDK5以后的新特性

A:自动装箱 基本类型--引用类型

B:自动拆箱 引用类型--基本类型

 

代码示例:

/*

JDK1.5版本以后出现的新特性。

 

*/

 

class IntegerDemo1 
{
public static void main(String[] args) 
{
//	Integer x = new Integer(4);
 
Integer x = 4;//自动装箱。//new Integer(4)
 
x = x/* x.intValue() */ + 2;//x+2:x 进行自动拆箱。变成成了int类型。和2进行加法运算。
//再将和进行装箱赋给x。
 
 
Integer m = 128;
Integer n = 128;
 
sop("m==n:"+(m==n));
 
Integer a = 127;
Integer b = 127;
 
sop("a==b:"+(a==b));//结果为true。因为a和b指向了同一个Integer对象。
//因为当数值在byte范围内容,对于新特性,如果该数值已经存在,则不会在开辟新的空间。
}
 
public static void method()
{
Integer x = new Integer("123");
 
Integer y = new Integer(123);
 
sop("x==y:"+(x==y));
sop("x.equals(y):"+x.equals(y));
}
 
public static void sop(String str)
{
System.out.println(str);
}
}


 

 

 

 

五. Date类:表示特定的瞬间,精确到毫秒。

构造方法:

Date():默认指当前系统时间。
Date(long  time):根据给定的毫秒值生成一个时间。

成员方法:

Date---long  
public long getTime():通过日期获取毫秒值。

 

Long---Date  
public void setTime(long time):通过毫秒值得到日期对象。

 

 

代码示例:

package Bao_Date;
 
import java.util.Date;
 
/*
 * Date:类 Date 表示特定的瞬间,精确到毫秒。
 * 
 * 构造方法:
 * Date():默认指当前系统时间。
 * Date(long time):根据给定的毫秒值生成一个时间。
 * 
 * 成员方法:
 * public long getTime():
 * public void setTime(long time):
 * 
 * Date -- long 通过日期获取毫秒值
 * Date d = new Date();
 * long time = d.getTime();
 * 
 * long -- Date通过毫秒值得到日期对象
 * long time = ???;
 * Date d = new Date(time);
 * 
 * Date d2 = new Date();
 * d2.setTime(time);
 */
public class DateDemo {
public static void main(String[] args) {
// 方式一
Date d = new Date();//默认当前系统时间
//Thu Sep 24 09:47:19 CST 2015
System.out.println(d);
System.out.println("****************");
//方式二
//1443059471381 刚才的毫秒值
//Sat Dec 21 15:42:41 CST 2013
long time = System.currentTimeMillis();
System.out.println(time);
Date d1 = new Date(time);
System.out.println(d1);
Date d2 = new Date(1443059471381L);
System.out.println(d2);
System.out.println("-------------------");
Date d3 = new Date();
d3.setTime(1443059471381L);
//1443059471381
//Thu Sep 24 09:51:11 CST 2015
System.out.println(d3.getTime());
System.out.println(d3);
}
}


 

 

 

 

 

六. DateFormat类:对日期进行格式化的类,提供了对日期进行格式化,和对字符串进行解析的功能。

构造函数:

     public SimpleDateFormat()

public SimpleDateFormat(String pattern)

 

 

成员方法:

Date---String
Public final String format(Date date):从日期到字符串的转换。

需要自己指定格式,常见的格式:

yyyyMMdd日 HH:mm:ss // yyyy-MM-dd  HH:mm:ss

yyyyMMdd

HH:mm:ss

 

String---Date
Public Date parse(String source):从字符串到日期的转换。

注意:如果是字符串到日期,你指定的格式必须和字符串的格式匹配。

Eg:

2015-08-27//yyyy-MM-dd

2015/08/27//yyyy/MM/dd

 

代码示例:

package Bao_Date;

 

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

 

/*

 * DateFormat:对日期进行格式化的类。提供了对日期进行格式化,和对字符串进行解析的功能。

 * 

 * Date -- String

 * public final String format(Date date)

 * 需要自己指定格式,常见的格式:

 * yyyy年MM月dd日 HH:mm:ss

yyyy年MM月dd

HH:mm:ss

yyyy-MM-dd HH:mm:ss

 * String -- Date

 * public Date parse(String source)

 * 注意:如果是字符串到日期,你指定的格式必须和字符串的格式匹配。

 * 

 * 2013-12-12

 * yyyy-MM-dd

 * 

 * 2013/11/11

 * yyyy/MM/dd

 */

public class DateFormatDemo {
public static void main(String[] args) throws ParseException {
//从Date--String
// 创建日期对象
Date d = new Date();
// Thu Sep 24 10:10:59 CST 2015
 System.out.println(d);
 
//2015年09月24日 10:10:59
//yyyy年MM月dd日 HH:mm:ss
//SimpleDateFormat(String pattern) 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str = sdf.format(d);
System.out.println(str);
System.out.println("************");
//从String--Date
String s = "2015-09-24 10:10:34";
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(s);
System.out.println(dd);
}
}


 

 

 

七. Calendar类:日历类,也是处理时间的。

功能:在日历字段和Calendar之间提供了转换功能,从Calendar获取到任何一个日历字段,然后,按照我需要的数据进行组合。

 

成员方法:

public static Calendar getInstance()

 

public int get (int field):(日历字段):根据给定的日历字段获取值

Eg: int year = c.get(Calendar.YEAR);  -->(年月日时分秒)

 

Public final void set (int year,int month,int date):(,,):给日历设定指定的年,月,日

 

Public abstract void add(int field,int amount)(日历字段,):给指定的日历字段添加或者减去给定的值。取决于值的正负。

 

 

 

代码示例:

package Bao_Date;
 
 
import java.util.Calendar;
 
/*
 * Calendar:日历类。也是处理时间的。
 * 
 * 在日历字段和Calendar之间提供了转换功能:
 * 从Calendar获取到任意一个日历字段。然后,按照我需要的数据进行组合。
 * 
 *
 abstract class Person
 * {
 * public static Person getPerson()
 * {
 * Person p = new Student();
 * return p;
 * }
 * }
 * 
 * class Student extends Person
 * {
 * ...
 * }
 * 
 * 成员方法:
 * public int get(int field):参数是日历字段。
 */
public class CalendarDemo {
public static void main(String[] args) {
// public static Calendar getInstance()
Calendar c = Calendar.getInstance();// 多态
 
// public static final int YEAR 年的字段
// public int get(int field):参数是日历字段。
int year = c.get(Calendar.YEAR);
// System.out.println(year);
 
// 月份
int month = c.get(Calendar.MONTH);
// 月份 0 - 11
// System.out.println(month + 1);
 
// 日
int date = c.get(Calendar.DATE);
// System.out.println(date);
 
// 时
int hour = c.get(Calendar.HOUR_OF_DAY);
// System.out.println(hour);
 
// 分
int minute = c.get(Calendar.MINUTE);
// System.out.println(minute);
 
// 秒
int second = c.get(Calendar.SECOND);
// System.out.println(second);
 
// 自己拼接
String s = year + "年" + (month + 1) + "月" + date + "日" + " " + hour
+ ":" + minute + ":" + ((second>9)?second:"0"+second);
System.out.println(s);
}
}


 

 

 

 

 

 

 

 

 



------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值