Java语法学习总结

1.数据类型

常见数据类型:

byte    //字节
int     //整数
float   //单精度浮点数
double  //双精度浮点数
char    //字符
boolean //布尔值
long    //长整型
short   //短整型
String  //字符串
 

2.常量定义方式

final int N = 100;

3.数据类型转换

int x = (int)'a';

4.运算符

+    //加
-    //减
*    //乘
/    //除
++   //自加
--   //自减
+=   //第一个数加上第二个数
-=   //第一个数减第二个数
*=   //第一个数乘以第二个数
/=   //第一个数除以第二个数
%=   //第一个数对第二个数取余

5.Java输入输出

1)小规模输入输出

模板:

import java.util.Scanner;

public class Small {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next(); //输入
        System.out.println(str); //输出
    }
}

 输入格式:

  String str1 = sc.next(); //遇到空格或者回车停下
  String str2 = sc.nextLine(); //读入一整行
  int n = sc.nextInt(); 
  double x = sc.nextDouble();
  char c = sc.next().charAt(0);

 输出格式:

  System.out.println("Hello World");
  System.out.print("Hello World");
  System.out.printf("\n Hello World \n");

2)大规模输入输出

模板:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Large {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int x = Integer.parseInt(br.readLine());//输入
        bw.write(x + " "); //输出

        bw.flush(); //清除缓冲区
    }
}

6.数组

数组定义(一维)

int f[] = new int[50];
double[] f = new double[50];
float[] f = new float[50];
String[] strs = new String[50];

数组常用API

数组不可变长

使用Aarrys需要加

import java.util.Arrays

数组长度

f.length

数组排序

Arrays.sort();

填充数组

Arrays.fill(int[] a, int val);

将数组转换成字符串

Arrays.toString(); 

多维数组转换成字符串

Arrays.deepToString();

7.字符串

String不能被修改。

如果需要修改字符串,可以使用StringBuilder和StringBuffer。

StringBuffer线程安全,速度较慢;

StringBuilder线程不安全,速度较快。

StringBuilder sb = new StringBuilder("Hello ");  // 初始化
sb.append("World");  // 拼接字符串

字符串常用API

翻转字符串

reverse():翻转字符串

字符串长度

str.length();

分割字符串

String[] strs = sc.nextLine().split(" ");

查找

找不到返回-1

//从前往后找
str.indexOf(char c);
str.indexOf(String str);
//从后往前找
str.lastIndexOf(char c);
str.lastIndexOf(String str);

判断两个字符串是否相等

str1.equals(str2);

比较两个字符串(按字典序)

负数表示小于;

0表示相等;

正数表示大于;

str1.compareTo(str2);

判断字符串是否以某个前缀开头

str1.startsWith(str2);

判断字符串是否以某个后缀结尾

str1.endsWith(str2);

去掉首尾的空格

str.trim();

大小写转换

toLowerCase();//全部用小写字符
toUpperCase();//全部用大写字符

替换

replace(char oldChar, char newChar);//替换字符
replace(String oldRegex, String newRegex);//替换字符串

截取字符串

返回[beginIndex, endIndex)中的子串

substring(int beginIndex, int endIndex);

将字符串转换为字符数组

toCharArray();

8.常用容器(类似于C++中 的STL容器)

链表(List)

接口

java.util.List<>

实现

java.util.ArrayList<>//变长数组
java.util.LinkedList<>//双链表
import java.util.ArrayList;
import java.util.LinkedList;

public class List {
    public static void main(String[] args) {
        LinkedList q = new LinkedList();
        ArrayList p = new ArrayList();
        q.add(2);
        q.add(3);
        q.add(4);
        p.add(5);
        p.add(6);
        p.add(7);
        System.out.println(q);
        System.out.println(p);
    }
}

 

函数

add()      //在末尾添加一个元素
clear()    //清空
size()     //返回长度
isEmpty()  //是否为空
get(i)     //获取第i个元素
set(i, val)//将第i个元素设置为val

栈(Stack)

java.util.Stack<>

实现 

import java.util.Scanner;
import java.util.Stack;

public class stack {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Stack<Integer> stk = new Stack<>();
        int m = sc.nextInt();

        while (m -- > 0) {
            String str = sc.next();

            if ("push".equals(str)) {
                int x = sc.nextInt();
                stk.push(x);
            }
            else if ("pop".equals(str)) {
                stk.pop();
            }
            else if ("empty".equals(str)) {
                if (stk.empty()) System.out.println("YES");
                else System.out.println("NO");
            }
            else {
                System.out.println(stk.peek());
            }
        }
    }
}

 

函数

push(); //压入元素
pop();  //弹出栈顶元素,并返回栈顶元素
peek(); //返回栈顶元素
size(); //返回长度
empty();//栈是否为空
clear();//清空

队列(Queue)

接口

java.util.Queue<>

实现

java.util.LinkedList<>  //双链表
java.util.PriorityQueue<> //优先队列
//默认是小根堆
new PriorityQueue<>(Collections.reverseOrder()) //大根堆
import java.util.PriorityQueue;

public class queue {
    public static void main(String[] args) {
        PriorityQueue hap = new PriorityQueue();
        hap.add(1);
        hap.add(2);
        hap.add(3);
        System.out.println(hap);
    }
}

 

函数

add();    //在队尾添加元素
remove();;//删除并返回队头
isEmpty();//是否为空
size();   //返回长度
peek();   //返回队头
clear();  //清空

Set

接口

java.util.Set<K>

实现

- java.util.HashSet<K>;//哈希表
- java.util.TreeSet<K>;//平衡树
import java.util.HashSet;
import java.util.TreeSet;

public class set {
    public static void main(String[] args) {
        TreeSet ts = new TreeSet();
        HashSet hs = new HashSet();

        ts.add(1);
        ts.add(2);
        ts.add(3);

        hs.add(4);
        hs.add(5);
        hs.add(6);

        System.out.println(ts);
        System.out.println(hs);
    }
}

 

函数

add();    //添加元素
contains();//是否包含某个元素
remove();//删除元素
size();//返回元素数
isEmpty();//是否为空
clear();//清空

/*java.util.TreeSet额外还有的函数:*/

ceiling(key);//返回大于等于key的最小元素,不存在则返回null
floor(key);//返回小于等于key的最大元素,不存在则返回null

Map

接口

java.util.Map<K, V>

实现

java.util.HashMap<K, V>;//哈希表
java.util.TreeMap<K, V>;//平衡树
import java.util.HashMap;
import java.util.TreeMap;

public class map {
    public static void main(String[] args) {
        HashMap<Integer, Integer> hm = new HashMap();
        TreeMap<Integer, Integer> tm = new TreeMap();

        hm.put(1, 1);
        hm.put(2, 2);
        hm.put(3, 3);

        tm.put(4, 4);
        tm.put(5, 5);
        tm.put(6, 6);

        System.out.println(hm);
        System.out.println(tm);
    }
}

 

函数

put(key, value); //添加关键字和其对应的值
get(key);        //返回关键字对应的值
containsKey(key);//是否包含关键字
remove(key);     //删除关键字
size();          //返回元素数
isEmpty();       //是否为空
clear();         //清空
entrySet();      //获取Map中的所有对象的集合
Map.Entry<K, V>; //Map中的对象类型
getKey();        //获取关键字
getValue();      //获取值

/* java.util.TreeMap<K, V>额外的函数:*/

ceilingEntry(key);//返回大于等于key的最小元素,不存在则返回null
floorEntry(key);  //返回小于等于key的最大元素,不存在则返回null

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值