java自学-常用api

API(Application Programming Interface),应用程序编程接口。Java API是JDK中提供给我们使用的类的说明文档。即jdk包里边写好的类,这些类将底层的代码实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可。所以我们可以通过查询API的方式,来学习Java提供的类,并得知如何使用它们。

例子
ArrayList<String> list = new ArrayList<String>();

ArrayList就是一个常用集合类,我们可以调用调用这个类的方法来进行业务操作,不需要我们自己去实现一个集合功能。下面介绍一些常用的类。

1.String类

java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如"abc")都可以被看作是实现此类的实例。类 String 中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串等
String s1 = "abc"; 
System.out.println(s1);
字符串不变:字符串的值在创建后不能被更改,值相同的字符串,内存中只有一个对象被创建
String s1 = "abc"; 
s1 += "d";
System.out.println(s1); // "abcd" 
// 值不变,不是说变量s1不能变,是字符串"abc"不变,内存中有"abc","abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"。

String s1 = "abc"; 
String s2 = "abc"; // 内存中只有一个"abc"对象被创建,同时被s1和s2共享。

java.lang包下的类不需要导入,可直接使用。创建String对象时,根据具体需要,调用构造方法创建

构造方法:

public String() :初始化新创建的 String对象,以使其表示空字符序列。
public String(char[] value) :通过当前参数中的字符数组来构造新的String。
public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
// 无参构造 
String str = new String(); 
// 通过字符数组构造 
char chars[] = {'a', 'b', 'c'}; 
String str2 = new String(chars); 
// 通过字节数组构造 
byte bytes[] = { 97, 98, 99 }; 
String str3 = new String(bytes);
常用方法:
public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。
public class String_Demo01 { 
public static void main(String[] args) { 
// 创建字符串对象 
String s1 = "hello"; 
String s2 = "hello"; 
String s3 = "HELLO"; 
// boolean equals(Object obj):比较字符串的内容是否相同 System.out.println(s1.equals(s2)); // true 
System.out.println(s1.equals(s3)); // false 
System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); 
//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 
System.out.println(s1.equalsIgnoreCase(s2)); // true 
System.out.println(s1.equalsIgnoreCase(s3)); // true 
 } 
}
public int length () :返回此字符串的长度。
public String concat (String str) :将指定的字符串连接到该字符串的末尾。
public char charAt (int index) :返回指定索引处的 char值。
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
 public class String_Demo02 { 
  public static void main(String[] args) {
   //创建字符串对象 
  String s = "helloworld"; 
  // int length():获取字符串的长度,其实也就是字符个数 
  System.out.println(s.length()); 
  System.out.println("‐‐‐‐‐‐‐‐"); 
  // String concat (String str):将将指定的字符串连接到该字符串的末尾. 
  String s = "helloworld"; 
 String s2 = s.concat("**hello");
 System.out.println(s2);// helloworld**hello
 // char charAt(int index):获取指定索引处的字符 
 System.out.println(s.charAt(0)); 
 System.out.println(s.charAt(1)); 
 System.out.println("‐‐‐‐‐‐‐‐"); 
 // int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1 
System.out.println(s.indexOf("l")); 
 System.out.println(s.indexOf("owo")); 
 System.out.println(s.indexOf("ak")); 
 System.out.println("‐‐‐‐‐‐‐‐"); 
 // String substring(int start):从start开始截取字符串到字符串结尾 
System.out.println(s.substring(0)); 
 System.out.println(s.substring(5)); 
 System.out.println("‐‐‐‐‐‐‐‐"); 
 // String substring(int start,int end):从start到end截取字符串。含start,不含end。 
System.out.println(s.substring(0, s.length())); 
System.out.println(s.substring(3,8)); 
 } 
}
public char[] toCharArray () :将此字符串转换为新的字符数组。
public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
public class String_Demo03 { 
public static void main(String[] args) { 
//创建字符串对象 
String s = "abcde"; 
// char[] toCharArray():把字符串转换为字符数组 
char[] chs = s.toCharArray(); 
for(int x = 0; x < chs.length; x++) { 
System.out.println(chs[x]); 
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); 
// byte[] getBytes ():把字符串转换为字节数组 
byte[] bytes = s.getBytes(); 
for(int x = 0; x < bytes.length; x++) { 
System.out.println(bytes[x]); 
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// 替换字母it为大写IT
String str = "itcast"; 
String replace = str.replace("it", "IT"); 
System.out.println(replace); // ITcast
 } 
}
public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
public class String_Demo03 { 
public static void main(String[] args) {
 //创建字符串对象 
String s = "aa|bb|cc"; 
String[] strArray = s.split("|"); // ["aa","bb","cc"] 
for(int x = 0; x < strArray.length; x++) {
 System.out.println(strArray[x]); // aa bb cc 
} 
} 
}

2.Arrays类

java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和搜索等。其所有方法均为静态方法,可直接调用
public static String toString(int[] a) :返回指定数组内容的字符串表示形式。
public static void main(String[] args) { 
// 定义int 数组 
int[] arr = {2,34,35,4,657,8,69,9}; 
// 打印数组,输出地址值 
System.out.println(arr); // [I@2ac1fdc4 
// 数组内容转为字符串 
String s = Arrays.toString(arr); 
// 打印字符串,输出内容 
System.out.println(s); // [2, 34, 35, 4, 657, 8, 69, 9] 
}
public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。
public static void main(String[] args) { 
// 定义int 数组 
int[] arr = {24, 7, 5, 48, 4, 46, 35, 11, 6, 2}; 
System.out.println("排序前:"+ Arrays.toString(arr)); // 排序前:[24, 7, 5, 48, 4, 46, 35, 11, 6, 2]
// 升序排序 
Arrays.sort(arr); 
System.out.println("排序后:"+ Arrays.toString(arr));// 排序后:[2, 4, 5, 6, 7, 11, 24, 35, 46, 48] 
}

3.ArrayList类

java.util.ArrayList 是大小可变的数组的实现,存储在内的数据称为元素,称为集合。此类提供一些方法来操作内部存储的元素。 ArrayList 中可不断添加元素,其大小也自动增长。
构造方法:
public ArrayList() :构造一个内容为空的集合。
ArrayList<String> list = new ArrayList<String>();
对于元素的操作,基本体现在——增、删、查。常用的方法有:
public boolean add(E e) :将指定的元素添加到此集合的尾部。
public E remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。
public E get(int index) :返回此集合中指定位置上的元素。返回获取的元素。
public int size() :返回此集合中的元素数。遍历集合时,可以控制索引范围,防止越界。
public class Demo01ArrayListMethod { 
public static void main(String[] args) { 
//创建集合对象 
ArrayList<String> list = new ArrayList<String>(); 
//添加元素 
list.add("hello"); 
list.add("world"); 
list.add("java"); 
//public E get(int index):返回指定索引处的元素 
System.out.println("get:"+list.get(0)); 
System.out.println("get:"+list.get(1)); 
System.out.println("get:"+list.get(2)); 
//public int size():返回集合中的元素的个数 
System.out.println("size:"+list.size()); 
//public E remove(int index):删除指定索引处的元素,返回被删除的元素 
System.out.println("remove:"+list.remove(0)); 
//遍历输出 
for(int i = 0; i < list.size(); i++){ 
System.out.println(list.get(i)); 
} 
} 
}

4.基本类型包装类

每种基本类型数据都对应有基本类型包装类

 

5.Random类

此类的实例用于生成伪随机数。 

 构造方法

public Random() :创建一个新的随机数生成器。 
Random r = new Random();
常用方法
public int nextInt(int n) :返回一个伪随机数,范围在 0 (包括)和 指定值 n (不包括)之间的int 值。 
//1. 导包 
import java.util.Random; 
public class Demo01_Random { 
public static void main(String[] args) { 
//2. 创建键盘录入数据的对象 
Random r = new Random(); 
for(int i = 0; i < 3; i++){ 
//3. 随机生成一个数据 
int number = r.nextInt(10); 
//4. 输出数据 
System.out.println("number:"+ number); 
} 
} 
}

6.Math类

java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具类,其所有方法均为静态方法,并且不会创建对象,可直接调用。
基本运算的方法:
public static double abs(double a) :返回 double 值的绝对值。 
double d1 = Math.abs(‐5); //d1的值为5
public static double ceil(double a) :返回大于等于参数的最小的整数。 
double d1 = Math.ceil(3.3); //d1的值为 4.0
public static double floor(double a) :返回小于等于参数最大的整数。
double d1 = Math.floor(3.3); //d1的值为3.0
public static long round(double a) :返回最接近参数的 long。(相当于四舍五入方法) 
long d1 = Math.round(5.5); //d1的值为6.0

 

posted @ 2019-12-19 15:15  自习小夫子  阅读(526)  评论(0编辑  收藏  举报