一、变量
(1)定义
变量必须先定义,才可以使用。不能重名。
变量定义的方式:
public class Main {
public static void main(String[] args) {
int a = 5;
int b , c = a;
int c = 10/2;
}
}
(2)内置数据类型
类型 | 字节数 | 表示范围 |
---|---|---|
byte | 1 | -128 ~ 127(- 2 ^ 7 ~ 2 ^ 7-1 ) |
short | 2 | -32768-32767(- 2 ^ 15 ~ 2 ^ 15-1 ) |
int | 4 | -2147483648-2147483647(- 2 ^ 31 ~ 2 ^ 31-1 ) |
long | 8 | -9223372036854775805-9223372036854775807(- 2 ^ 63 ~ 2 ^ 63-1 ) |
float | 4 | -3.40E+38 ~ +3.40E+38(- 2 ^ 128 ~ 2 ^ 127 ,有效6~7位) |
double | 8 | -1.79E+308 ~ +1.79E+308(- 2 ^ 1024 ~ 2 ^ 1023,有效15位 ) |
boolean | true/false(默认false ) | |
char | 2 | 0~65535( \u0000 ~ \uffff ) |
(3)常量
使用final修饰:
final int N = 110;
类型转化:
显示转化:int x = (int)'A';
隐式转化:double x = 12, y = 4 * 3.3;
二、运算符
A = 10 , B = 20;
运算符 | 描述 | 实例 |
---|---|---|
+ | 把两个数相加 | A + B = 30 |
- | 从第一个数中减去第二个数 | A - B = -10 |
* | 把两个数相乘 | A * B = 200 |
/ | 分子除以分母 | B / A = 2 |
% | 取模运算符,向零整除后的余数,注意余数可能为负数 | B % A = 0 |
++ | 自增运算符 | A++:先取值后加1;++A:先加1后取值 |
– | 自减运算符 | A–:先取值后减1;–A:先减1后取值 |
+= | 第一个数加上第二个数 | A = A + B 可以简写为 A += B |
-= | 第一个数减去第二个数 | A = A - B 可以简写为 A -= B |
*= | 第一个数乘以第二个数 | A = A * B 可以简写为 A *= B |
/= | 第一个数除以第二个数 | A = A / B 可以简写为 A /= B |
%= | 第一个对第二个数取余数 | A = A % B 可以简写为 A %= B |
三、表达式
(1)整数的加减乘除四则运算
public class Main {
public static void main(String[] args) {
int a = 6 + 3 * 4 / 2 - 2;
System.out.println(a);
int b = a * 10 + 5 / 2;
System.out.println(b);
System.out.println(23 * 56 - 78 / 3);
}
}
(2)浮点数(小数)的运算
public class Main {
public static void main(String[] args) {
double x = 1.5, y = 3.2;
System.out.println(x * y);
System.out.println(x + y);
System.out.println(x - y);
System.out.println(x / y);
}
}
(3)整型变量的自增、自减
public class Main {
public static void main(String[] args) {
int a = 1;
int b = a ++ ;
System.out.println(a + " " + b);
int c = ++ a;
System.out.println(a + " " + c);
}
}
四、输入
(1)效率较低,输入规模较小时使用
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
String str = sc.next(); // 读入下一个字符串,遇到空格终止
String str = sc.nextLine(); //读入一行字符串
int x = sc.nextInt(); // 读入下一个整数
float y = sc.nextFloat(); // 读入下一个单精度浮点数
double z = sc.nextDouble(); // 读入下一个双精度浮点数
String line = sc.nextLine(); // 读入下一行
}
}
(2)效率较高,输入规模较大时使用。注意需要抛异常
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
System.out.println(str);
}
}
五、输出
(1)效率较低,输出规模较小时使用
public class Main {
public static void main(String[] args) throws Exception {
System.out.println(123); // 输出整数 + 换行
System.out.println("Hello World"); // 输出字符串 + 换行
System.out.print(123); // 输出整数
System.out.print("yxc\n"); // 输出字符串
System.out.printf("%04d %.2f\n", 4, 123.456D); // 格式化输出,float与double都用%f输出
}
}
System.out.printf()中不同类型变量的输出格式:
(1) int:%d
(2) float: %f, 默认保留6位小数
(3) double: %f, 默认保留6位小数
(4) char: %c, 回车也是一个字符,用’\n’表示
(5) String: %s
(2)效率较高,输出规模较大时使用。注意需要抛异常
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write("Hello World\n");
bw.flush(); // 需要手动刷新缓冲区
}
}
六、判断语句
基本语句if-else
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a > 5) {
System.out.printf("%d is big!\n", a);
System.out.printf("%d + 1 = %d\n", a, a + 1);
}
else System.out.printf("%d - 1 = %d\n", a, a - 1);
}
}
同c/c++
七、循环语句
(1)while循环
先判断,后执行
while (i < 10) {
System.out.println(i);
i ++ ;
}
(2)do while循环
先执行,后判断,至少执行一次循环
do {
System.out.println("y!");
} while (y < 1);
(3)for循环
基本思想:把控制循环次数的变量从循环体中剥离。
for (int i = 0; i < 10; i ++ )
System.out.println(i);
for (int i = 1, j = 10; i < j; i ++, j -- ) {
sum += i * j;
}
同c/c++
八、数组
1.java中数组的定义[]是写在数组名前面,而且可以用变量定义数组长度,数组自动初始化为0
2.多维数组长度可以不一样
3.多维数组不支持直接比较大小和相加减
(1)定义
TYPE[] x
public class Main {
public static void main(String[] args) {
//一维数组
int[] a = new int[5], b; //b是空数组
float[] f = new float[n]; //与c/c++不同的是java可以用变量定义数组长度
double[] d = new double[5];
char[] c = new char[5];
String[] strs = new String[5]; //每个元素初始化为null
//多维数组
int[][] a = new int[5][4]; // 大小为5的数组,每个元素是含有4个整数的数组。
int[][][] b = new int[3][4][5]; // 将所有元素的初值为0
// 大小为3的数组,它的每个元素是含有4个数组的数组,这些数组的元素是含有5个整数的数组
}
}
(2)初始化
public class Main {
//一维数组
public static void main(String[] args) {
int[] a = {1,2,3}; // 含有3个元素的数组,元素分别是0, 1, 2
int[] b = new int[3]; // 含有3个元素的数组,元素的值均为0
char[] d = {'a', 'b', 'c'}; // 字符数组的初始化
//多维数组
int[][] a = { // 三个元素,每个元素都是大小为4的数组
{0, 1, 2, 3}, // 第1行的初始值
{4, 5, 6}, // 第2行的初始值
{8, 9, 10, 11} // 第3行的初始值
};
for (int i = 0; i < 4; i ++ )
a[0][i] = 0;
}
}
(3)访问数组
通过下标访问数组。
public class Main {
public static void main(String[] args) {
//一维数组
int[] a = {5, 2, 0}; // 数组下标从0开始
System.out.printf("%d %d %d\n", a[0], a[1], a[2]);
a[0] = 1;
System.out.println(a[0]);
//多维数组
for (int i = 0; i < 5; i ++ ) { // 输出二维数组
for (int j = 0; j < 4; j ++ ) {
System.out.printf("%d ", a[i][j]);
}
}
}
(4)常用API
1.length:返回数组长度,注意不加小括号
int[] a = {1,2,3};
System.out.println(a.length);
2.Arrays.sort():数组从小到大排序
int[] a = {5,2,6,9,7};
Arrays.sort(a);
3.Arrays.fill(int[] a, int val):填充数组(只能是一维数组)
int[] a = new int[5];
Arrays.fill(a,0); //a中元素全部赋0值
4.Arrays.toString():将数组转化为字符串
import java.util.Scanner;
import java.util.Arrays;
public class _12 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0 ; i < n ; i ++)
a[i] = sc.nextInt();
System.out.println(Arrays.toString(a));
}
}
测试结果:
5.Arrays.deepToString():将多维数组转化为字符串
import java.util.Arrays;
public class _12 {
public static void main(String[] args){
int[][] a = {
{1,2,3,4},
{2,3,4,5},
{3,4,5,6}
};
System.out.println(Arrays.deepToString(a));
}
}
测试结果:
6.数组不可变长
7.使用Arrays需要import java.util.Arrays
九、字符串
常用ASCII值:‘A’- 'Z’是65 ~ 90,‘a’ - 'z’是97 - 122,0 - 9是 48 - 57。
String类
(1)初始化
String a = "Hello World";
String b = "My name is ";
String x = b; // 存储到了相同地址
String c = b + "abc"; // String可以通过加号拼接
String d = "My age is " + 18; // int会被隐式转化成字符串"18"
String str = String.format("My age is %d", 18); // 格式化字符串,类似于C++中的sprintf
String money_str = "123.45";
double money = Double.parseDouble(money_str); // String转double
int money = Integer.parseInt(money_str); //String转int
//int转String
int money = 123;
String money_str = "" + money
Integer money = 123;
String money_str = money.toString();
只读变量,不能修改,例如:
String a = "Hello ";
a += "World"; // 会构造一个新的字符串
(2)访问String中的字符
String str = "Hello World";
for (int i = 0; i < str.length(); i ++ ) {
System.out.print(str.charAt(i)); // 只能读取,不能写入
}
(3)常用API
1.length():返回长度
String str = "Hello world!";
System.out.println(str.length());
2.split(String regex):分割字符串
import java.util.Arrays;
public class _12{
public static void main(String[] args){
String str = "Hello world!";
String[] s = str.split(" ");
System.out.println(Arrays.toString(s));
}
}
样例结果:
3.indexOf(char c)、indexOf(String str)、lastIndexOf(char c)、lastIndexOf(String str):查找,找不到返回-1
public class _12{
public static void main(String[] args){
String str = "Hello world!";
System.out.println(str.indexOf('e')); //返回某一字符第一次出现的下标
System.out.println(str.indexOf("wo")); //返回某一字符串第一次出现的下标
System.out.println(str.lastIndexOf('l')); //返回某一字符最后一次出现的下标
System.out.println(str.lastIndexOf("ld")); //返回某一字符串最后一次出现的下标
}
}
样例结果:
4.equals():判断两个字符串是否相等,注意不能直接用==
String str = "Hello,world!";
System.out.println(str.equals("Hello,world!"));
5.compareTo():判断两个字符串的字典序大小,负数表示小于,0表示相等,正数表示大于
String str = "abc";
System.out.println(str.compareTo("abc"));
6.startsWith():判断是否以某个前缀开头
String str = "Hello world!";
System.out.println(str.startsWith("He"));
7.endsWith():判断是否以某个后缀结尾
String str = "Hello world!";
System.out.println(str.endsWith("ld!"));
8.trim():去掉首尾的空白字符
String str = " Hello world! \n";
System.out.println(str.trim());
9.toLowerCase():全部用小写字符
String str = "Hello world!";
System.out.println(str.toLowerCase());
10.toUpperCase():全部用大写字符
String str = "Hello world!";
System.out.println(str.toUpperCase());
11.replace(char oldChar, char newChar):替换字符
String str = "Hello world!";
System.out.println(str.replace('l' , 'L'));
12.replace(String oldRegex, String newRegex):替换字符串
String str = "Hello world!";
System.out.println(str.replace("ll" , ""));
13.substring(int beginIndex, int endIndex):返回[beginIndex, endIndex)中的子串
String str = "Hello world!";
System.out.println(str.substring(1,3));
System.out.println(str.substring(1)); //返回从下标1到末尾的字符串
14.toCharArray():将字符串转化成字符数组**
import java.util.Arrays;
public class _12{
public static void main(String[] args){
String str = "Hello world!";
char[] ch = str.toCharArray(); //更方便地定义字符串
for(char c : ch)
System.out.print(c);
}
}
样例结果:
(4)输入与输出
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.next(); // 输入字符串,遇到空格、回车等空白字符时停止输入
String str2 = sc.nextLine(); // 输入一整行字符串,遇到空格不会停止输入,遇到回车才会停止
System.out.println(str1); // 可以直接输出
System.out.printf("%s\n", str2); // 也可以格式化输出,用 %s 表示字符串
}
}
(5)StringBuilder、StringBuffer
1.String不能被修改,如果打算修改字符串,可以使用StringBuilder和StringBuffer。
2.StringBuffer线程安全,速度较慢;StringBuilder线程不安全,速度较快。
public class _12{
public static void main(String[] args){
StringBuilder sb = new StringBuilder("Hello "); // 初始化
sb.append("World"); // 拼接字符串
System.out.println(sb);
for (int i = 0; i < sb.length(); i ++ ) {
sb.setCharAt(i, (char)(sb.charAt(i) + 1)); // 读取和写入字符
}
System.out.println(sb);
sb.reverse(); //修改了原来值
System.out.println(sb);
}
}
十、函数
参考c/c++
十一、类与接口
(1)类
参考c/c++
1.静态变量 全局访问
2.静态函数只能调用静态函数和静态变量
3.普通函数可以调用静态和普通函数及变量
4.类的继承:extends
class M extends N{}
5.类的多态:函数名相同,参数不同。调用相同函数接入不同参数,返回结果不同。
(2)接口
1.interface与class类似,主要用来定义类中所需包含的函数。
2.接口也可以继承其他接口,一个类可以实现多个接口。
3.调用接口必须实现接口内全部函数。
4.接口继承:extends
interface B extends A {
public void test();
}
5.接口多态:implements
class C implements B {
public void test1();
}
十二、常用容器
参考 acwing
学习网站acwing