JAVA API学习
有JDK_API_1.6、jdk1.8中文版两个文档,翻译了java API中的注释 IOS电脑需要下载工具才能打开 api使用:打开->显示->索引->输入java提供的类
字符串 String
原理:char[] ch = {‘a’,‘b’,‘c’}; 特点:方法不能改变String本身 方法: equals:判断字符串是是否一样,大小写敏感 split:切割 replace:替换 substring:字符串截取 trim:删除字符串两端的空格 length:获取字符串长度 equalsIgnoreCase:判断是否一样,忽略大小写 indexOf:获取指定内容在字符串中出现的位置 isEmpty:判断字符串是否为空 lastIndexOf:获取要匹配内容在字符串中最后出现的位置 == 和 equals 区别 == 比较基本数据类型比较的是值,比较引用数据类型比较的是地址值 equals 是Object类中的方法,基本数据类型无法调用 equals默认使用==号,重写之后一般比较的是内容
public class StringDemo {
public static void main ( String[ ] args) {
String str = "abc" ;
str = "111" ;
System. out. println ( str. equals ( "222" ) ) ;
System. out. println ( str. equals ( "111" ) ) ;
System. out. println ( str. equalsIgnoreCase ( "ABc" ) ) ;
String str1 = "===${PWD}====" ;
String newStr = str1. replace ( "${PWD}" , "123456" ) ;
System. out. println ( str1) ;
System. out. println ( newStr) ;
String str2 = "a12, 3, 123, 45, 13" ;
String[ ] arr = str2. split ( "," ) ;
System. out. println ( str2) ;
for ( String s : arr) {
System. out. println ( s) ;
}
String str3 = "aaa112312323aaa" ;
System. out. println ( str3. substring ( 3 ) ) ;
System. out. println ( str3. substring ( 3 , 5 ) ) ;
System. out. println ( str3. length ( ) ) ;
System. out. println ( str3. indexOf ( "123" ) ) ;
System. out. println ( str3. lastIndexOf ( "123" ) ) ;
String str4 = null;
String str5 = "ytrhb" ;
System. out. println ( str4. isEmpty ( ) ) ;
System. out. println ( str5. isEmpty ( ) ) ;
}
}
接口和抽象类(了解)
接口:规则、规范行为。只能有抽象方法,一个类可以同时实现多个接口,必须重写所有抽象方法 接口与接口是继承,接口与类是实现 接口是对继承的补充 interface implements 定义一个接口: public interface Inter { //默认修饰符 public abstract 可以省略 public abstract void show(); } 实现一个接口: public Student implements Inter { public abstract void show() { System.out.println(“show…”); } } 案例:父接口中的抽象方法就是规则,规范返回值、参数类型、方法名 public interface Inter { //抽象方法没有方法体 public abstract int show(); //规则1 void print(int a); //规则2 } public class Imp implements Inter { public int show() { //遵循了规则1 重写 //…任意代码 } public void print(int a) { //遵循了规则2 重写 //…任意代码 } } 抽象类:不能创建对象的类(不能new),与普通类的唯一区别是可以用抽象方法。子类可以继承抽象类,但必须重写抽象方法 abstract:定义一个抽象类 public abstract class BaseCase { public BaseCase() {} public abstract void info(); public void getData() { System.out.println(“getData…”); } } java提供了八大基本数据类型对应的引用数据类型 boolean-------Boolean byte-------Byte char-------Character short-------Short int-------Integer long-------Long float-------Float double-------Double 静态方法:不需要创建对象,直接类名.调用 int i = Integer.parseInt(“123”); System.out.println(i* 10);
ArrayList
ArrayList:有序,可重复 HashSet:无序,不可重复 特点:长度可变数组,提供很多方便的方法 ArrayList list = new ArrayList(); 泛型:规定集合存储的数据类型 常用API: 添加数据:add(…) 取出数据:get(…) 获取list集合的大小:size() 移除数据:remove(…) isEmpty(…):判空函数 contains(…):判断是否包含某数据 set(…):修改某个元素 遍历集合: 普通for 增强for 迭代器:iterator():获取迭代器;hasNext():迭代器通过指针下移判断是否有数据;next():取数据 toString(): toString()是来自Object类中的方法,当我们使用System.out.println(s)输出时自动会调用toString(); 重写toString()就是为了方便打印
public class ArrayListDemo {
public static void main ( String[ ] args) {
ArrayList list = new ArrayList ( ) ;
System. out. println ( list) ;
System. out. println ( list. size ( ) ) ;
list. add ( 1 ) ;
list. add ( "1000" ) ;
list. add ( true ) ;
System. out. println ( list) ;
ArrayList< String> list1 = new ArrayList < > ( ) ;
list1. add ( "ahhfdh" ) ;
list1. add ( "1453" ) ;
System. out. println ( list1) ;
ArrayList< Integer> list2 = new ArrayList < > ( ) ;
list2. add ( 222 ) ;
list2. add ( 222 ) ;
list2. add ( 1 ) ;
System. out. println ( list2) ;
System. out. println ( list2. get ( 0 ) ) ;
System. out. println ( list2. get ( 1 ) ) ;
System. out. println ( list2. get ( 2 ) ) ;
}
}
public class ArrayListAPI {
public static void main ( String[ ] args) {
ArrayList< String> list1 = new ArrayList < > ( ) ;
list1. add ( "ahhfdh" ) ;
list1. add ( "1453" ) ;
list1. add ( "4444" ) ;
list1. add ( "14gffgh53" ) ;
System. out. println ( list1) ;
list1. remove ( 0 ) ;
System. out. println ( list1) ;
list1. remove ( "4444" ) ;
System. out. println ( list1) ;
System. out. println ( list1. isEmpty ( ) ) ;
list1. clear ( ) ;
System. out. println ( list1. isEmpty ( ) ) ;
System. out. println ( list1. contains ( "4444" ) ) ;
list1. set ( 0 , "000000" ) ;
System. out. println ( list1) ;
}
}
public class Student {
private String name;
private int age;
public Student ( ) {
}
public Student ( String name, int age) {
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
}
public class ArrayListCustom {
public static void main ( String[ ] args) {
ArrayList< Student> list = new ArrayList < > ( ) ;
Student s1 = new Student ( "张三" , 11 ) ;
Student s2 = new Student ( "刘备" , 21 ) ;
Student s3 = new Student ( "关羽" , 31 ) ;
Student s4 = new Student ( "吕小布" , 41 ) ;
list. add ( s1) ;
list. add ( s2) ;
list. add ( s3) ;
list. add ( s4) ;
System. out. println ( list) ;
}
}
public class ArrayListLoop {
public static void main ( String[ ] args) {
ArrayList< String> list1 = new ArrayList < > ( ) ;
list1. add ( "ahhfdh" ) ;
list1. add ( "1453" ) ;
list1. add ( "4444" ) ;
list1. add ( "14gffgh53" ) ;
for ( int i= 0 ; i< list1. size ( ) ; i++ ) {
System. out. println ( list1. get ( i) ) ;
}
System. out. println ( "---------" ) ;
for ( String element : list1) {
System. out. println ( element) ;
}
System. out. println ( "---------" ) ;
Iterator< String> it = list1. iterator ( ) ;
while ( it. hasNext ( ) ) {
String element = it. next ( ) ;
System. out. println ( element) ;
}
}
}