Java基本
eclipse编译器:
1、JAVA 8指的是JDK1.8(又称JDK8.0) 2、打开左侧的项目栏:eclipse左侧的项目栏是项目资源管理器;在窗口-显示视图,找到项目资源管理器,打开即可;windows->show viewer->Project Explorer
代码规范:
一、输入输出:
1、static Scanner in=new Scanner(new BufferedInputStream(System.in));
2、static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
3、java中的printf打印double时,也用%f,没有l //L
4、java中的printf打印long时,用%d,没有ll
5、in.hasNext(); // 判断缓存区中还有没有数据,有返回true, 否则等待输入。
二、main函数之前的东西
1、static int N=(int)1e6+7;
2、static InputReader in=new InputReader();
3、static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
三、数组:
1、static int[] a=new int[7]; //数组定义 //定义一偏名为a的int空间
3、初始化:Arrays.fill(int[] arr, int x); //将arr内的每个元素赋值为x
注意事项:
1、基本类型的大写是包装类,需要使用equals来判断,注意容器中用的是包装类
测试的时候写成==可以通过是因为Integer在-128~127的范围内==和equals的判断结果是一样的,因为Integer内一个叫IntegerCache的静态类
Integer和int比较时会自动拆箱所以用==也没问题,注意两个包装类比较用equals比较值,而不是用==比较地址
测试的时候写成==可以通过是因为Integer在-128~127的范围内==和equals的判断结果是一样的,因为Integer内一个叫IntegerCache的静态类
Integer和int比较时会自动拆箱所以用==也没问题,注意两个包装类比较用equals比较值,而不是用==比较地址
包装类运算时会转回基本类型
输入输出:
快读:当输入规模达1e6时,用快读,其他时候没必要
static InputReader in=new InputReader();
static class InputReader{ //快速输入 //静态内部类,主类不能静态,主类只能公开、抽象、final
BufferedReader buf;
StringTokenizer tok; //字符串分隔解析类
InputReader() {
buf=new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext() {
while(tok==null || !tok.hasMoreElements()) {
try {
tok=new StringTokenizer(buf.readLine());
} catch (Exception e) {
return false;
}
}
return true;
}
String next() {
if(hasNext()) return tok.nextToken();
return null;
}
String nextLine() { String str=null; try { str=buf.readLine(); } catch (Exception e) { e.printStackTrace(); } return str; }
int nextInt() {
return Integer.parseInt(next()); //parseInt解析字符串,并返回一个整数
//next()是读取下一个字符串
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
BigInteger nextBigInteger() {
return new BigInteger(next());
}
BigDecimal nextBigDecimal() { //高精度
return new BigDecimal(next());
}
}
快写
public static void main(String[] args) throws IOException{ //异常要么抛出(throws),要么抓住(try-catch)
int n=2;
//快速输出:
PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
out.println(n);
out.printf("%d",n);
out.flush(); //将输出缓冲区清空
//注意要在最后刷新输出缓冲区, 就是记得加上out.flush(),否则会什么也不输出
//out.close(); //关闭输入流,和flush不一样,flush比较好
}
注:
字符串:
1、输入字符:char ch=in.next().charAt(0); 2、String str=in.next(); //输入字符串 3、Sting str=in.nextLine(); //读入一行 4、str.equals(str2); //比较函数;直接用==的话,要str和str2的地址和内容都相等才返回true 5、char[] ch=s.toCharArray(); // 字符串转换为字符数组,字符数组可以访问下标,且能 ++和 --
sort:
sort: Arrays.sort(a,1,n+1); //排序,基本类型,默认升序
1、自定义比较算子(4种写法): Arrays.sort(a,1,n+1,(x,y)->(y-x)); //自定义比较算子不能是基本类型 //升序是x-y,降序是y-x
2、匿名内部类:
Arrays.sort(a,1,n+1,new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o2-o1;
}
});
3、定义一个比较类实现Comparator接口的compare方法
4、在定义类时,实现Comparator接口的compareTo方法
集合【容器】用collections.sort 排序结构体【类】
class L{
int a,b;
}
public static void main(String[] args) throws IOException{ x.a=3; x.a=in.nextInt(); out.print(x.a); out.flush(); }
类数组
import java.util.Arrays;
class Point implements Comparable<Point>{
int x,y;
//自定义的比较函数,此例中先x后y从小到大排序
@Override
public int compareTo(Point o) {
return x!=o.x? x-o.x: y-o.y;
}
}
public class Main {
public static void main(String[] args) {
//Java里的数组要先new数组,再new每个元素,不是数组有了每个元素也就有了
Point[] p = new Point[3];
for(int i = 0;i < p.length;++i){
p[i] = new Point();
}
//不用上面的for把每个元素new出来直接进行下面的赋值会空指针的
//其实应该在Point里重载有参的构造函数,直接在new的时候初始化,这样代码简洁些
p[0].x = 3;
p[0].y = 3;
p[1].x = 1;
p[1].y = 4;
p[2].x = 3;
p[2].y = 1;
//sort还可以在第2、3个参数上指定排序起止
Arrays.sort(p);//先x后y从小到大排序
for(Point t:p){
System.out.println(t.x + " " + t.y);
}
}
}
