Java基础语法

队列Queue

Queue基础函数

  • 声明:Queue Q = new LinkedList<>()

  • Q.add():队尾加入元素

  • Q.peek():获取队首元素,不修改队列

  • Q.poll():获取队首元素,同时该元素出队

  • Q.remove():同poll,但如果队列为空,remove抛出异常,poll返回null

  • Q.isEmpty():队列是否为空

  • Q.size():队列的元素个数

import java.util.*;

public class Main {
    static class Node  {
        int x,y;
        Node(){}
        Node(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
    static int dir[][] = {{0,1},{0,-1},{1,0},{1,0}};
    public static void main(String[] args) {
        Queue<Node> Q = new LinkedList<>();
        Q.add(new Node(1,2));
        while(!Q.isEmpty()){
            Node p = Q.poll();
            for(int k=0;k<4;k++){
                int px = p.x + dir[k][0];
                int py = p.y + dir[k][1];
                // ...
            }
        }
    }
}

优先队列

需要实现Comparator

import java.util.*;

public class Main {
    static class Node  {
        int x,y;
        Node(){}
        Node(int x,int y){
            this.x = x;
            this.y = y;
        }
        public String toString(){
            return x + " " + y;
        }
    }

    public static void main(String[] args) {
        Comparator<Node> cmp = (o1, o2) -> {
            if(o1.x!=o2.x) return o1.x - o2.x; // x正序
            return o2.y - o1.y; // y倒序
        };
        Queue<Node> PQ = new PriorityQueue<>(cmp);
        PQ.add(new Node(1,2));
        PQ.add(new Node(2,5));
        PQ.add(new Node(2,6));
        PQ.add(new Node(1,3));

        while(!PQ.isEmpty()){
            System.out.println(PQ.poll());
        }
        /*
        1 3
        1 2
        2 6
        2 5
         */
    }
}

集合Set

Set基础函数

需要重写equals和hashCode

  • 声明:Set set = new HashSet<>()
  • set.add():插入元素
  • set.remove():删除元素
  • set.size():当前元素个数
import java.util.*;

public class Main {
    static class Node  {
        int x,y;
        Node(){}
        Node(int x,int y){
            this.x = x;
            this.y = y;
        }
        public boolean equals(Object o){
            Node t = (Node) o;
            return t.x == x && t.y == y;
        }
        public int hashCode(){
            return 1;
        }
    }
    public static void main(String[] args) {
        Set<Node> set = new HashSet<>();
        set.add(new Node(1,2));
        set.add(new Node(1,2));
        set.add(new Node(1,3));
        set.remove(new Node(1,2));
        System.out.println("set:" + set.size());
        for(Node t : set){
            System.out.println("set=" + t.x+","+t.y);
        }
        /*
        set:1
		set=1,3
		 */
    }
}

映射Map

Map基础函数

需要重写equals和hashCode

  • 声明:Map<Node, Integer> map = new HashMap<>()
  • map.put:插入元素
  • map.get:查询元素
  • map.size:当前元素个数
import java.util.*;

public class Main {
    static class Node  {
        int x,y;
        Node(){}
        Node(int x,int y){
            this.x = x;
            this.y = y;
        }
        public boolean equals(Object o){
            Node t = (Node) o;
            return t.x == x && t.y == y;
        }
        public int hashCode(){
            return 1;
        }
    }
    public static void main(String[] args) {
        Map<Node, Integer> map = new HashMap<>();
        map.put(new Node(1,2),3);
        map.put(new Node(1,2),4);
        map.put(new Node(1,3),5);

        System.out.println("Map size="+map.size());
        System.out.println(map.get(new Node(1,2)));
        System.out.println(map.get(new Node(1,3)));
        /*
        Map size=2
        4
        5
         */
    }
}

可变数组ArrayList

ArrayList基础函数

  • ArrayList arrayList = new ArrayList<>()
  • arrayList.add():插入元素
  • arrayList.remove(int index):删除第index个元素
  • arrayList.clear():清空数组
import java.util.*;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(4);
        arrayList.remove(0); // 删除第0个元素

        System.out.println("size="+arrayList.size());
        for (int i : arrayList){
            System.out.println("Arraylist="+ i);
        }
        System.out.println(arrayList.contains(1));
        arrayList.clear();
    }
}

二维ArrayList

尽量用ArrayList数组

import java.util.*;

public class Main {
    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> arrayLists = new ArrayList<>();
        int tot = 0;
        for(int i=0;i<4;i++){
            ArrayList<Integer> t = new ArrayList<>();
            for(int j=0;j<4;j++){
                t.add(tot++);
            }
            arrayLists.add(t);
        }
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                System.out.print(arrayLists.get(i).get(j)+" ");
            }
            System.out.println();
        }
        /*
        0 1 2 3 
        4 5 6 7 
        8 9 10 11 
        12 13 14 15
         */
    }
}

ArrayList数组

import java.util.*;

public class Main {
    static final int N = (int) 1e5+10;

    public static void main(String[] args) {
        ArrayList<Integer> f[] = new ArrayList[N];
        for(int i=1;i<=1e5;i++) f[i] = new ArrayList<>();
    }
}

排序Sort

Comparator函数

// 完整写法
Comparator<Node> cmp1 = new Comparator<Node>() {
    // 重写compare方法
    public int compare(Node o1, Node o2) {
        return 0;
    }
};
// 简洁写法
Comparator<Node> cmp2 = (o1, o2) -> {
    return 0;
};

ArrayList结构体排序

  • 排序函数:a.sort(cmp)
import java.util.*;

public class Main {
    static class Node  {
        int x,y;
        Node(){}
        Node(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
    public static void main(String[] args) {
        Comparator<Node> cmp = (o1, o2) -> {
            if(o1.x!=o2.x) return o1.x - o2.x; // x 升序
            return o2.y - o1.y; // y 降序
        };
        ArrayList<Node> a = new ArrayList<>();
        a.add(new Node(1,13));
        a.add(new Node(3,11));
        a.add(new Node(4,12));
        a.add(new Node(4,13));
        a.add(new Node(4,11));
        a.add(new Node(2,14));

        a.sort(cmp);
        for(int i=0;i<a.size();i++){
            System.out.printf("%d %d\n", a.get(i).x, a.get(i).y);
        }
        /*
        1 13
        2 14
        3 11
        4 13
        4 12
        4 11
         */
    }
}

Array结构体排序

  • 排序函数:Arrays.sort(a,0,7,cmp),对数组a按照cmp方法从0到7排序,不包括7
import java.util.*;

public class Main {
    static class Node  {
        int x,y;
        Node(){}
        Node(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
    public static void main(String[] args) {
        Comparator<Node> cmp = (o1, o2) -> {
            if(o1.x!=o2.x) return o1.x - o2.x; // x 升序
            return o2.y - o1.y; // y 降序
        };
        Node a1[] = new Node[10];
        a1[0] = new Node(1,14);
        a1[1] = new Node(3,12);
        a1[2] = new Node(4,11);
        a1[3] = new Node(2,13);
        a1[4] = new Node(2,19);
        a1[5] = new Node(2,12);
        a1[6] = new Node(2,14);

        Arrays.sort(a1,0,7,cmp);
        for(int i=0;i<7;i++){
            System.out.printf("%d %d\n", a1[i].x, a1[i].y);
        }
        /*
        1 14
        2 19
        2 14
        2 13
        2 12
        3 12
        4 11
         */
    }
}

字符串 StringBuffer

String是不能变动的

StringBuffer是可操作的

  • str.reverse():反转字符串

  • str.replace(1,2,“abc”):在[1,2)范围替换字符串

  • str.substring(1,3):返回子字符串

public class Main {
    public static void main(String[] args) {
        String s = "1234";
        String re = new StringBuffer(s).reverse().toString();

        StringBuffer str = new StringBuffer(s);
        str.replace(1,2,"abc");
        System.out.println(str);
        String str1 = str.substring(1,3);
        System.out.println(str1);

        char[] ch = {'a','b','c'};
        String str3 = new String(ch);
        System.out.println(str3);

        char[] ch1 = s.toCharArray();
        for(char x : ch1){
            System.out.print(x);
        }
        System.out.println();
    }
}

文件流

  • System.setIn(new FileInputStream(“in.txt”)):输入
  • System.setOut(new PrintStream(“out.txt”)):输出
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        try{
            System.setIn(new FileInputStream("in.txt"));
            System.setOut(new PrintStream("out.txt"));
            // 完全形式:System.setOut(new PrintStream(new FileOutputStream("out.txt")));
        }
        catch (Exception e){}
        Scanner in = new Scanner(System.in);
        String s1 = in.next();
        System.out.println(s1);
    }
}
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值