黑马程序员-----java基础 API学习

来源:互联网 发布:mysql表空间不足 编辑:程序博客网 时间:2024/06/13 09:39

-----------android培训java培训、java学习型技术博客、期待与您交流!------------ 

如何学习API

1,首先了解一下这个API是干嘛的

2,开始学习API的构造方法

3,学习这个API的普通方法

4,测试

一,Object类

是类层次结构的顶层类,是所有类的根类,超类。  所有的类都直接或者间接的继承自Object类。

Object类的方法

1:to String();

public String toString():返回对象的字符串表示形式。
组成:包名...类名@内存地址值的十六进制

2:equals()
public boolean equals(Object obj):
默认比较的是对象的地址值是否相同。
一般,会重写该方法。按照自己的需求进行比较。


==和equals()的区别?
==:
比较基本类型:比较的是基本类型的值是否相同。
比较引用类型:比较的是引用类型的地址值是否相同。

equals():
比较引用类型,默认比较的是引用类型的地址值是否相同。
如果类重写了该方法,那就得按照重写后的规则进行比较。


二,String类

字符串:多个字符组成的一串数据。
构造方法:
1:String s = new String();
2:String s = new String(byte[] bys);
3:String s = new String(byte[] bys,int index,int length);
4:String s = new String(char[] chs);
5:String s = new String(char[] chs,int index,int length);
6:String s = new String(String str);
7:String s = "hello";
字符串的特点及面试题
1:字符串一旦被赋值,就不能改变。
注意:字符串的值不能改变,没有说引用变量不能改变。
2:面试题:
1:String s = new String("hello")和String s = "hello"的区别。
2:请写出结果:
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2);   //  false
System.out.println(s1.equals(s2));//true


String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3==s4);      //false
System.out.println(s3.equals(s4));//true


String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6);//true
System.out.println(s5.equals(s6));//true
(4)成员方法(补齐方法意思)
A:判断功能
boolean equals(Object obj)   判断字符串内容是否相同,区分大小写
boolean equalsIgnoreCase(String str)判断字符串内容是否相同,不区分大小写
boolean contains(String str)判断字符串对象是否包含给定字符串
boolean startsWith(String str)判断字符串对象是否以给定字符串开始
boolean endsWith(String str)判断字符串对象是否以给定字符串结束
boolean isEmpty()判断字符串对象是否为空
B:获取功能
int length()获取字符串长度
char charAt(int index)返回字符串中给定索引处字符
int indexOf(int ch)返回指定字符在此字符串第一次出现的索引

int indexOf(String str)返回指定字符串在此字符串第一次出现的索引
int indexOf(int ch,int fromIndex)返回在此字符串中第一次出现指定字符的索引,从指定的索引开始搜索。
int indexOf(String str,int fromIndex)返回在此字符串中第一次出现指定字符串的索引,从指定的索引开始搜索。
String substring(int start)截取字符串。返回从指定位置开始截取后的字符串。
String substring(int start,int end)截取字符串。返回从指定位置开始到指定位置结束截取后的字符串。
C:转换功能
byte[] getBytes()把字符串转换成字节数组。
char[] toCharArray()把字符串转换成字符数组。
static String copyValueOf(char[] chs)把字符数组转换成字符串。
static String valueOf(char[] chs)把字符数组转换成字符串。
static String valueOf(int i)把int(基本类型)转换成字符串。
String toLowerCase()把字符串变成小写。
String toUpperCase()把字符串变成大写。
String concat(String str)拼接字符串。


D:其他功能
a:替换功能
String replace(char oldChar,char newChar)
String replace(String oldString,String newString)


b:切割功能
String[] split(String regex)

c:去除两端空格功能
String trim()

d:字典顺序比较功能
int compareTo(String str)
int compareToIgnoreCase(String str) 

三 Arrays工具类的使用
(1)Arrays是针对数组操作的工具类。
(2)成员方法:
public static String toString(数组):把数组变成字符串。
public static void sort(数组):对数组进行排序。
public static int binarySearch(int[] arr,int value):二分查找


四 System的方法
(1)系统类,提供了静态的变量和方法供我们使用。
(2)成员方法:
public static void exit(int value):退出jvm,非0表示异常退出。
public static long currentTimeMillis():返回当前系统时间的毫秒值。
和1970 年 1 月 1 日午夜之间的时间差


五 StringBuffer
1字符个数可以发生改变的字符串类。字符串缓冲区类。
2构造方法:
A:StringBuffer()
B:StringBuffer(int capacity)
C:StringBuffer(String str)
3成员方法:
A:添加功能
append
insert


B:删除功能
deleteCharAt
delete

C:替换功能
replace

D:截取功能
substring

E:反转功能
reverse
4案例:
字符串反转。


七 基本类型包装类
1基本类型的数据我们只能使用值,不能做更多的操作。为了方便我们操作,
  java就把每种基本类型进行了包装。提供方法供我们使用。
2基本类型和包装类的对应关系
byte
short
int Integer
long 
float
double
char Character
boolean
3Integer构造方法
A:Integer i = new Integer(int num);
B:Integer i = new Integer(String s);
注意:s必须是一个由数字字符组成的字符串。
4String和int类型的转换
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)
5JDK5以后的新特性
A:自动装箱 基本类型--引用类型
B:自动拆箱 引用类型--基本类型


八 Date   Date 表示特定的瞬间,精确到毫秒。

  构造方法:
   Date():默认指当前系统时间。
   Date(long time):根据给定的毫秒值生成一个时间。
  
  成员方法:
  public long getTime()
   public void setTime(long time)

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

Date -- String
  public final String format(Date date)

String -- Date
  public Date parse(String source)
  注意:如果是字符串到日期,你指定的格式必须和字符串的格式匹配。


案列

1统计字符串中大写,小写,数字字符出现的次数

<pre name="code" class="java">public class StringTest {public static void main(String[] args) {String s = "Hello12345World";// 定义三个统计变量int bigCount = 0;int smallCount = 0;int numberCount = 0;// 遍历字符串。for (int x = 0; x < s.length(); x++) {char ch = s.charAt(x);// 判断是属于哪种范围的if (ch >= 'A' && ch <= 'Z') {bigCount++;} else if (ch >= 'a' && ch <= 'z') {smallCount++;} else if (ch >= '0' && ch <= '9') {numberCount++;}}System.out.println("大写:" + bigCount);System.out.println("小写:" + smallCount);System.out.println("数字:" + numberCount);}}
<span style="white-space:pre"></span>2把字符串的首字母大写,其他小写<pre name="code" class="java">public class StringTest {public static void main(String[] args) {String s = "helloWorldJAVA";String s1 = s.substring(0, 1);String s2 = s.substring(1);String s3 = s1.toUpperCase().concat(s2.toLowerCase());System.out.println(s3);}}
3,大串中截取小串输入小串次数
class Test{public static void main(String [] args){String st="hanbasdnbafllgnbahjnbakqqqqlnbaxi";int index = st.indexOf("nba");int cishu =0;while(true){if (st.contains("nba")){st = st.substring(index+3);index = st.indexOf("nba");cishu++;}elsebreak;}System.out.println(cishu);}<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space: normal;"></span></span>
                                             
0 0