长文慎点,Java学习笔记(四)

这篇博客是作者的Java学习记录,主要内容包括正则表达式的简单实践和双向链表的实现,还分享了一个基础的GUI贪吃蛇游戏的代码示例。
摘要由CSDN通过智能技术生成

这只是一篇我自己学习java的流水账笔记,包含了以下内容,全是基础代码示例,希望对正在学习java的小伙伴有帮助

  • java GUI贪吃蛇:[Daily] Snake
  • java正则表达式:Regular Expression
  • 双向链表:[Daily]Two-way loopback linked list

Regular Expression

Simple practice

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
 
public class Test1 {
   
 
    public static void main(String[] args) {
   
        //初步认识Pattern & Matcher
        /*
        print("初步认识Pattern & Matcher");
         
        print("abc".matches("..."));
        print("a3456677a".replaceAll("\\d", "-"));
         
        Pattern p = Pattern.compile("[a-z]{3}");
        Matcher m = p.matcher("abc");
        print(m.matches());
         
        print("");
        */
        //初步认识 . * + ? 
        /*
        print("初步认识 . * + ?");
         
        print("TEST .");//一个任意字符
        print("aaa".matches("a.a"));
        print("ata".matches("a.a"));
 
        print("TEST *");//0个或多个
        print("".matches("a*"));
        print("a".matches("a*"));
 
        print("TEST +");//一个或多个
        print("".matches("a+"));//false
        print("a".matches("a+"));
 
        print("TEST ?");//0个或一个
        print("".matches("a?"));
        print("a".matches("a?"));
        print("aa".matches("a?"));//false
         
        print("TEST others");
        print("234567899".matches("\\d{3,100}"));
        print("192.168".matches("\\d{1,3}\\.\\d{1,3}"));
        print("192".matches("[0-2][0-9][0-5]"));
         
        print("");
        */
        //认识范围
        /*
        print("TEST []");
        print("a".matches("[abc]"));
        print("a".matches("[^abc]"));//取反false
         
        print("A".matches("[a-zA-Z]"));
        print("A".matches("[a-z]|[A-Z]"));
        print("A".matches("[a-z[A-Z]]"));
         
        print("R".matches("[A-Z&&[RGB]]"));//交集
        */
        //认识 \d \D \s \S \w \W  \
        /*
        print("认识 \\d \\D \\s \\S \\w \\W  \\");
        print("4".matches("\\d"));// \d  [0-9]
        print("4".matches("\\D"));// \D  [^0-9]
     
        print(" ".matches("\\s"));// \s 空白字符 [\t\n\x0B\f\r]
        print(" ".matches("\\S"));// \s 反空白字符 [^\s]
         
        print("a".matches("\\w"));// \w [a-zA-Z_0-9]
        print("a".matches("\\W"));// \w [^\w]
         
        print("\\".matches("\\\\"));// 正则"\\\\" 表示  \
        print("abc8881&".matches("[a-z]{1,3}\\d+[%^&]+"));
        */
        //boundary 边界匹配
        /*
        print("边界匹配");//^表示开头 $表示结束
        print("hello world".matches("^h.*d$"));
        print("hello world".matches(".*ld$"));
        print("hello world".matches("^h[a-z]{1,4}\\b.*"));
         */
 
        //practice
        /*
        print("practice");
         
        print("aaa 8888c".matches(".*\\b\\d{4}."));
        print("745998763@qq.com".matches("\\w+@\\w+\\.[A-Za-z]{2,14}"));
        print("a-".matches("\\w[-]"));
        print(".".matches("[\\w[.-]]"));
        print("+".matches("[+]+"));
        print("+q".matches("[+].+"));
        print("-".matches("[-A-Za-z0-9]"));
        */
        //matches find looking at
        /*
        print("matches find looking at");
         
        Pattern p = Pattern.compile("[a-z]{2,4}");
        Matcher m = p.matcher("ab-abd-abcf-OO");
        print(m.matches());
        m.reset();
        print(m.find());
        print(m.start() + "-" + m.end());
         
        print(m.find());
        print(m.start() + "-" + m.end());
         
        print(m.find());       
        print(m.start() + "-" + m.end());
         
        print(m.find());
        //print(m.start() + "-" + m.end());
 
        print(m.lookingAt());
        print(m.lookingAt());
        print(m.lookingAt());
        print(m.lookingAt());*/
         
        //replacement
        /*
        print("replacement");
        Pattern p = Pattern.compile("sakura",Pattern.CASE_INSENSITIVE);
        String str = "sakura SAKURA saKURA SAkura ILoveSakura YouLoveSakuraToo";
        Matcher m = p.matcher(str);
        //print(m.replaceAll("SAKURA"));
        int i = 1;
        StringBuffer buf = new StringBuffer();
        while(m.find()){
            if(i%2 == 0){
                m.appendReplacement(buf, "sakura");
            }else {
                m.appendReplacement(buf, "SAKURA");
            }
            i++;
        }
        m.appendTail(buf);
        print(buf);*/
         
        //group
        /*
        print("group");
        Pattern p = Pattern.compile("([a-z]{1,3})([0-9]{1,2})");
        Matcher m = p.matcher("qwe09 asd78 saj67");
        while(m.find()){
            print(m.group(1));
            print(m.group(2));
        }*/
         
        //
         
        //print("a".matches("[a-z&&[^bc]]"));//差集
         
        //Greedy Reluctant Possessive
 
        /*
        String s = "aahsjkhjdskka";
        //Matcher m = Pattern.compile("[a-z]+?").matcher(s);
        //Matcher m = Pattern.compile("[a-z]*?").matcher(s);
        //Matcher m = Pattern.compile("[a-z]??").matcher(s);
        //Matcher m = Pattern.compile("[a-z]{2,5}").matcher(s);
        Matcher m = Pattern.compile("[a-z]{2,5}?").matcher(s);
        while(m.find()){
            print(m.group());
            print(m.start()+"-"+m.end());
        }*/
         
        //logic operators
        //print("a".matches("[([a-z]|[0-9])[a-z][0-9]]+"));
        //print("0g9ag8".matches("(([a-z]|[0-9])[a-z][0-9])+"));
         
        /*
        Matcher m = Pattern.compile(".{3}(?=a)").matcher("444a66b");
        while(m.find()){
            print(m.group());
        }*/
         
        //back reference
        /*
        Matcher m = Pattern.compile("([a-z][a-z])\\1").matcher("ababcdef");
        while(m.find()){
            print(m.group());//abab 和第一个组相同
        }*/
         
        /*Matcher m = Pattern.compile("([a-z]([a-z]))\\2").matcher("abbcddefr");
        while(m.find()){
            print(m.group());//abb cdd和第二个组相同,第二个组被第一个组包含
        }*/
         
        print("JaVa".matches("(?i)(java)"));
         
    }
     
         
    public static void print(Object o){
   
        System.out.println(o);
    }
}

[Daily]Two-way loopback linked list

public class NodeLinkedList{
   
     
    public static void main(String[] args){
   
        List list = new List(500);
        print(list.length);
        Count3Quit(list);
         
    }
 
    public static void Count3Quit(List list){
   
        int countNum = 0;
        Node node = list.head;
        while(list.length > 0){
   
            countNum ++;
            if(countNum == 3){
   
                countNum = 0;
                list.deleteNode(node);
            }
            node = node.next;
        }
        print(list.head.id);
        ///print(list.tail.id);
    }
 
    public static void print(Object o){
   
        System.out.println(o);
    }
     
}
 
class Node{
   
    int id;
    Node prev;
    Node next;
}
 
 
class List{
   
    int length = 0; 
    Node head;
    Node tail;
     
    List(int n){
   
        for(int i=0; i<n; i++){
   
            addNode();
        }
    }
     
    public void addNode(){
   
        Node node = new Node();
        if(length == 0){
   
            head = tail = node;
            print("The first Node added!");
        }else{
   
            head.prev = node;
            node
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码狂魔v

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值