一个Java文件的文件名必须和文件中的主类的名字相同(就是被public修饰的类),一个Java文件只能有一个public类,但可以有多个不带public的类。如果文件中不止一个类,而且没有public类,文件名可与任一类名一致
throws Exception放在方法后边,是表示的是本方法不处理异常,交给被调用处处理(如果你不希望异常层层往上抛,你就要用throws Exception) ,而且被调用处必须处理。
ACM和自己写输入输出的刷题模式
import java.util.*;
public class Main { //类名要是Main!!!
public static void main(String[] args) throws Exception{ //main函数要是静态static的!!!
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.nextLine());
System.out.println(s);
}
}
Java读取输入
Scanner scan = new Scanner(System.in);
//判断是否还有输入
scan.hasNext();
scan.hasNextLine(); //常用
//读取字符串和和数字
String str2 = scan.nextLine();
double x = scan.nextDouble();
int x = scan.nextInt();
//关闭输入,输入结束记得关闭
scan.close();
//读取一行字符串
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// nextLine方式接收字符串
System.out.println("nextLine方式接收:");
// 判断是否还有输入
if (scan.hasNextLine()) {
String str2 = scan.nextLine();
System.out.println("输入的数据为:" + str2);
}
scan.close();
}
}
//读取Double数字
import java.util.Scanner;
class RunoobTest {
public static void main(String[] args) {
System.out.println("请输入数字:");
Scanner scan = new Scanner(System.in);
double sum = 0; int m = 0;
while (scan.hasNextDouble()) {
double x = scan.nextDouble();
//注意nextDouble只读取数字,下一行若还有要读的,需要用nextLine把回车读了
m = m + 1;
sum = sum + x;
}
System.out.println(m + "个数的和为" + sum);
System.out.println(m + "个数的平均值是" + (sum / m)); scan.close();
}
}
Java打印
System.out.println();
System.out.print();
Java的StringTokenizer类。 构造一个用来解析 str 的 StringTokenizer 对象。java 默认的分隔符是空格("")、制表符(\t)、换行符(\n)、回车符(\r)。
//构造函数
StringTokenizer st=new StringTokenizer(String str);
StringTokenizer st=new StringTokenizer(String str, string delim);
//方法
int countTokens(); //返回nextToken方法被调用的次数
boolean hasMoreTokens(); //返回是否还有元素(字符串)
String nextToken(); //返回从当前位置到下一个分隔符的字符串。
Java科学计数法表示法,默认是double类型。
double a=1e9+7; //a=10^9+7
一个刷题中容易出现的错误: 子集 - 子集 - 力扣(LeetCode) (leetcode-cn.com) 即:直接将后面还会变化的对象的引用当做结果。
class Solution {
List<Integer> t = new ArrayList<Integer>();
List<List<Integer>> ans = new ArrayList<List<Integer>>();
public List<List<Integer>> subsets(int[] nums) {
dfs(0, nums);
return ans;
}
public void dfs(int cur, int[] nums) {
if (cur == nums.length) {
ans.add(new ArrayList<Integer>(t)); //注意这里需要引用t用新建一个对象再加入到res中
//否则之后对象还会变,加入到res中的引用,其指向的List<Integer>对象就会变。
return;
}
t.add(nums[cur]);
dfs(cur + 1, nums);
t.remove(t.size() - 1);
dfs(cur + 1, nums);
}
}
java运算符优先级: == 优先级高于 &(位运算符),记得加括号。
0与任何数异或运算的结果还是这个数本身。
当要求的返回结果类型是int[][],一般先得到List<int[]>类型的结果,再将其转为int[][]类型。
//多态的体现
List<int[]> res=new ArrayList<>(); //List<int[]> res里面用int[]即可,不需要包装类
int[][] a=res.toArray(new int[res.size()][]);
注意二者的区别!包装类的判断是否相等应该用equals,基础数据类型用==。
else if(q1.peek()==q2.peekFirst()) q2.pollFirst();
else if(q1.peek().equals(q2.peekFirst())) q2.pollFirst();
new LinkedList<List<Integer>>();这个新建的对象看起来复杂,其实本质还是使用LinkedList类的构造函数LinkedList<>(),只是限制了LinkedList对象内部的元素是List<Integer>类型的。
//错 接口无对象,不能初始化
List<List<Integer>> res1=new List<>();
//错 LinkedList<>()没法转换到List<Integer>
List<List<Integer>> res2=new LinkedList<LinkedList<Integer>>();
List<List<Integer>> res3=new LinkedList<List<Integer>>(); //对
List<List<Integer>> res4=new ArrayList<List<Integer>>(); //对
List<List<Integer>> res5=new LinkedList<>(); //对
List<List<Integer>> res6=new ArrayList<>(); //对
int[] arr1 = new int[]{2,10,5,8,3};
Integer[] arr2 = {2,10,5,8,3};
Arrays.sort(arr2);
return new int[] {x, y};
return new int[0];
int a=12;
//右移一位相当于除2
int b=a>>1; //b=6
b>>=1;
//左移一位相当于乘2
int c=a<<1; //c=24
//无符号右移
System.out.println(5>>3);//结果是0
System.out.println(-5>>3);//结果是-1
System.out.println(-5>>>3);//结果是536870911
n>>>=1;
//按位xx不是只处理一位,而是每一位都处理
//按位与&
//按位或|
//按位异或^
//按位取反~
用Stack对象初始化LinkedList对象
List<List<Integer>> res=new LinkedList<>();
Stack<Integer> l=new Stack<>();
//用Stack对象生成LinkedList对象
res.add(new LinkedList<>(l));
拷贝数组给一个新数组
//已有数组int[] nums
int[] numsSorted = new int[nums.length];
System.arraycopy(nums, 0, numsSorted, 0, nums.length);
//静态方法,形参分别是原数组,原数组copy起始点,目标数组,目标数组粘贴起点,要copy的数组段的长度。
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
Java的int基本数据类型,Integer.MIN_VALUE 和 Integer.MAX_VALUE。 最小值Integer.MIN_VALUE ,即-2147483648。 最大值Integer.MAX_VALUE,即2147483647。