-
Scanner类
Scanner类可以实现键盘输入数据,到程序中
Scanner类是引用数据类型
导入Scanner(为什么String类不需要导入?因为:只有
java.lang
包下的内容不需要导包,其它的包都需要手动导入)import java.util.Scanner; public class DemoScanner { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); String string = scanner.next(); System.out.println(num); System.out.println(string); } }
-
匿名对象
匿名对象就是只有右边的对象,没有左边的名字和赋值运算符
new 类名称();
注意事项:匿名对象只能使用唯一的一次,下次再用不得不再new一个对象
import java.util.Scanner; public class Anonymous { public static void main(String[] args) { // 匿名对象 int num = new Scanner(System.in).nextInt(); System.out.println(num); // 匿名对象当做参数 getNum(new Scanner(System.in)); } public static void getNum(Scanner scanner){ System.out.println(scanner.next()); } }
-
Random类
Random类可以生成一个随机数:
import java.util.Random;
Random r = new Random();
获取一个随机int数字(范围是int所有范围,有正负两种):
int num = random.nextInt()
获取一个随机int数字(参数代表了范围,顾前不顾尾):
int num = random.nextInt(100)
取值的范围是[0, 99)// 猜数字小游戏 import java.util.Scanner; import java.util.Random; public class RandomGame { public static void main(String[] args) { int random = new Random().nextInt(100); System.out.println(random); while (true) { int num = new Scanner(System.in).nextInt(); if (num > random){ System.out.println("greater than"); } else if (num < random){ System.out.println("less than"); } else { System.out.println("bingo"); break; } } } }
-
ArrayList类
ArrayList
集合的长度是可以随意变化的对于
ArrayList
来说,有一个尖括号代表泛型泛型:就是装在集合中的元素的类型(统一)
注意事项:
- 泛型只能是引用类型,不能是基本类型
- 对于
ArrayList
集合来说,直接打印得到的不是地址值,而是存储的内容,如果内容为空,那么打印[]
import java.util.ArrayList; public class DemoArrayList { public static void main(String[] args) { // 创建一个ArrayList ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("甲"); // arrayList.add(10); // 泛型已经指明是String类型,况且只能是引用类型 System.out.println(arrayList); // [甲] } }
ArrayList
的常用方法:public boolean add(E e)
:向集合中添加元素,元素的类型应与泛型一致,返回值是boolean类型public E get(int index)
:根据索引值从集合中获取元素,返回元素类型与泛型一致public E remove(int index)
:根据索引值从集合中删除元素,返回元素类型与泛型一致public int size()
:获取集合中元素的长度,返回int类型import java.util.ArrayList; public class ArrayListMethod { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList(); // 添加 boolean bool = arrayList.add("甲"); System.out.println(bool); // true // 获取 String str = arrayList.get(0); System.out.println(str); // 甲 // 获取大小 int count = arrayList.size(); System.out.println(count); // 1 // 删除 String delete = arrayList.remove(0); System.out.println(delete); // 甲 } }
如果需要往集合中存储基本数据类型,那么就必须使用基本数据类型对应的包装类
包装类与String类一样,都在java.long包下,所以可以直接使用
Byte --> byte
Short --> short
Integer --> int
// 特殊Long --> long
Float --> float
Double --> double
Character --> char
// 特殊Boolean --> boolean
从JDK 1.5开始,支持自动装箱,自动拆箱:基本类型 <----> 包装类型
import java.util.ArrayList; public class WrapperClass { public static void main(String[] args) { // 指定集合中的泛型是Integer ArrayList<Integer> arrayList = new ArrayList<>(); arrayList.add(100); int num = arrayList.get(0); System.out.println(num); // 100 } }
做一个小题目:
// 定义以指定格式打印集合的方法,格式参照:{元素@元素@元素@元素} import java.util.ArrayList; public class ArrayListPrint { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("2019"); arrayList.add("-02"); arrayList.add("-14"); System.out.println(arrayList); // [2019, -02, -14] arrayListPrint(arrayList); } public static void arrayListPrint(ArrayList<String> arrayList){ System.out.print("{"); for (int i = 0; i < arrayList.size(); i++){ String str = arrayList.get(i); System.out.print(str); if (i != arrayList.size() - 1){ System.out.print("@"); } } System.out.print("}"); // {2019@-02@-14} } }
-
String类
程序中所有的双引号字符串,都是String类的对象
字符串的特点:
- 字符串的内容永不可变(正是因为字符串不可改变,所以字符串是可以共享使用的)
- 字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组
字符串的常见3+1种创建方式:
public class DemoString { public static void main(String[] args) { // 最简单的方式创建一个字符串 String str = "johny"; // 使用空参构建 String str1 = new String(); System.out.println(str1); // "" // 根据字符数组的内容,来创建对应的字符串 char[] charArray = {'A', 'B', 'C'}; String str2 = new String(charArray); System.out.println(str2); // "ABC" // 根据字节数组的内容,来创建对应的字符串 byte[] byteArray = {97, 98, 99}; String str3 = new String(byteArray); System.out.println(str3); // "abc" } }
字符串常量池:
字符串内容的比较
==运算符是进行对象的地址值比较,如果需要比较字符串的内容,那么可以使用
equals()
方法注意事项:
equals()
具有对称性,也就是 a.equals(b) 和 b.equals(a) 是一样的- 如果比较双方中一个常量和一个变量,推荐把常量写在前面:
"abc".equals(a)
,因为前面的字符串不能是null,否则会出现NullPointerException
equalsIgnoreCase()
在比较时忽略大小写
public class StringEquals { public static void main(String[] args) { String strA = "abc"; String strB = "abc"; char[] arrayChar = {'a', 'b', 'c'}; String strC = new String(arrayChar); System.out.println(strA.equals(strB)); // true System.out.println(strA.equals(strC)); // true // 推荐写法 System.out.println("abc".equals(strA)); // true // 空指针异常 String strD = null; // System.out.println(strD.equals(strA)); // NullPointerException // equalsIgnoreCase() System.out.println("ABC".equalsIgnoreCase(strA)); // true } }
字符串常用方法
int length()
:获取字符串当中含有的字符个数,就是字符串长度String concat(String str)
:将当前字符串和参数字符串进行拼接char charAt(int index)
:获取指定索引值位置的字符int indexOf(String/char args)
:查找参数字符串/字符在当前字符串中的第一次索引位置,如果没有找到就返回 -1public class StringGet { public static void main(String[] args) { String strA = "abc"; char[] arrayChar = {'A', 'B', 'C'}; String strB = "ABC"; // 返回字符串长度 System.out.println(strA.length()); // 3 // 字符串拼接 System.out.println(strA.concat(strB)); // abcABC // 返回指定索引值位置上的字符 System.out.println(strA.charAt(1)); // b // 查找参数字符串在当前字符串中的第一次索引位置,没有找到就返回-1 System.out.println(strA.indexOf("bc")); // 1 System.out.println(strA.indexOf("d")); // -1 } }
字符串的截取(切片)
String substring(int index)
:截取从参数位置一直到字符串末尾,返回新字符串String substring(int begin, int end)
:截取从begin开始,到end结束,顾前不复尾public class StringSubstring{ public static void main(String[] args){ String strA = "hello, world!"; // 截取 System.out.println(strA.substring(0, 5)); // hello } }
字符串中关于转换的方法
char[] toCharArray()
:将当前字符串拆分成为字符数组作为返回值,返回字符数组byte[] getBytes()
:获取当前字符串底层的字节数组,返回的是字节数组的地址值(引用)String replace(CharSequence oldString, CharSequence newString)
:将出现的所有老字符串替换成新字符串,返回替换之后的结果字符串public class StringConvert{ public static void main(String[] args){ String strA = "中国"; // 返回字符数组 System.out.println(strA.toCharArray()); // 中国 // 返回字节数组(返回的是对象的引用) byte[] byteArray1 = strA.getBytes(); System.out.println(byteArray1); // [B@1e643faf for (int i = 0; i < strA.length(); i++){ System.out.print(byteArray1[i]); // -28-72 if (i == strA.length() - 1){ System.out.println(); } } byte[] byteArray2 = "abc".getBytes(); System.out.println(byteArray2); // [B@6e8dacdf for (int i = 0; i < "abc".length(); i++){ System.out.print(byteArray2[i]); // 979899 if (i == "abc".length() - 1){ System.out.println(); } } // 字符串替换 System.out.println(strA.replace("中", "爱")); // 爱国 } }
字符串的切割
String[] split(String regex)
:按照参数的规则,将当前字符串分割为多个部分,返回字符串数组的地址值(引用)注意事项:
- split方法的参数其实是一个正则表达式,如果按照英文句点
.
进行切分,必须写\\.
public class StringSplit{ public static void main(String[] args){ String strA = "my name is johny"; String[] strArray = strA.split(" "); // {"my", "name", "is", "johny"} System.out.println(strArray); // [Ljava.lang.String;@6e8dacdf // 遍历打印 for (int i = 0; i < strArray.length; i++){ System.out.print(strArray[i]); // mynameisjohny } // 特殊写法 String strB = "a.b.c"; System.out.println(strB.split("\\.")); // [Ljava.lang.String;@7a79be86 for (int i = 0; i < strB.split("\\.").length; i++){ System.out.print(strArray[i]); // mynameisjohny } } }
-
静态static关键字
static关键字概述
static修饰成员变量(用static修饰后类似于类属性)
如果一个成员变量使用了static关键字,那么这个成员变量不再属于自己,而是属于所在的类,多个对象共享这个变量
推荐写法:类名.变量名
public class Student { private String name; private int age; static String classRoom = "101_class_room"; static int idCounter = 0; // 构造方法 public Student(String name, int age){ this.name = name; this.age = age; ++Student.idCounter; } public static void main(String[] args) { Student student1 = new Student("tom", 20); System.out.println(student1.name + " " + Student.classRoom + " " + Student.idCounter); // tom 101_class_room 1 Student student2 = new Student("johny", 18); System.out.println(student2.name + " " + Student.classRoom + " " + Student.idCounter); // johny 101_class_room 2 } }
static修饰成员方法(类似于类方法)
如果没有static关键字,那么必须创建对象,然后通过对象调用方法
对于静态方法来说,可以通过对象名进行调用,也可以直接通过类名称进行调用(推荐)
使用对象名调用时,会被javac翻译成:类名.方法名
注意事项:
- 静态不能直接访问非静态(因为在内存中是【先】有的静态内容,【后】有的非静态内容)
- 静态方法中不能用this
public class StaticMethod{ private int num; static int staticNum; // 实例方法 public void method(){ System.out.println("this is obj method"); // 非静态可以访问静态 System.out.println(staticNum); // 0 } // 类方法 public static void classMethod(){ System.out.println("this is class method"); // 静态不能访问非静态 // System.out.println(num); } public static void main(String[] args){ StaticMethod sm = new StaticMethod(); // 使用类名调用类方法 StaticMethod.classMethod(); sm.method(); } }
静态代码块
用途:用来一次性的对类变量进行赋值
注意事项:
- 当第一次用到当前类时,静态代码块执行唯一的一次
- 静态内容总是优先于非静态,所以静态代码块比构造方法先执行
public class StaticCodeBlock{ static{ // 静态代码块 System.out.println("static code block execution"); } // 构造方法 public StaticCodeBlock(){ System.out.println("constructor execution"); } public static void main(String[] args) { StaticCodeBlock codeOne = new StaticCodeBlock(); StaticCodeBlock codeTwo = new StaticCodeBlock(); } } // 执行结果 // static code block execution // 执行唯一一次 // constructor execution // constructor execution
-
数组工具类Arrays
java.util.Arrays
是一个与数组相关的工具类,里面提供了大量静态方法,用来实现常见的数组操作public static String toString(参数数组)
:将参数数组转换为字符串返回(按照默认格式:[element1, element2, element3 …])public static void sort(参数数组)
:将当前数组进行排序,默认升序import java.util.Arrays; public class DemoArrays { public static void main(String[] args) { int[] arrayNum = {5, 1, 6, 3, 9}; String[] arrayString = {"bbb", "ccc", "aaa"}; // toString() System.out.println(Arrays.toString(arrayNum)); // [5, 1, 6, 3, 9] // sort() Arrays.sort(arrayNum); System.out.println(Arrays.toString(arrayNum)); // [1, 3, 5, 6, 9] Arrays.sort(arrayString); System.out.println(Arrays.toString(arrayString)); // [aaa, bbb, ccc] } }
-
数学工具类Math
java.util.Math
类是数学相关的工具类,里面提供了大量的静态方法,完成数学运算相关的操作public static double abs(double num)
:获取绝对值public static double ceil(double num)
:向上取整public static double floor(doubel num)
:向下取整public static long round(double num)
:四舍五入
public class DemoMath { public static void main(String[] args) { double num = -12.24; // 绝对值 System.out.println(Math.abs(num)); // 12.24 double // 向上取整 System.out.println(Math.ceil(num)); // 12.0 double // 向下取整 System.out.println(Math.floor(num)); // 13.0 double // 四舍五入 System.out.println(Math.round(num)); // 12 long } }
JAVA常用API-第一部分
最新推荐文章于 2020-08-04 17:37:12 发布