Day12_23

作者: yangyangyjj | 来源:发表于2016-12-24 11:37 被阅读0次

String ,StringBuffer,StringBuilder

package Day12_22_00;

import java.util.Arrays;
import java.util.Comparator;
//最具逼格的冒泡排序
public class Test002
{
    /*
     * 泛型(generic)-让类型不再是程序中的硬代码(hard code)
     * 
     * <T extends Comparable<T>>--T代表的是引用类型,不是基本类型 此处的extends不是继承,
     * 而是泛型限定.   限定T的类型必须是Comparable接口的子类型
     * 
     */
    public static <T extends Comparable<T>> void bubbleSort(T[] array)
    {
        // 通用冒泡排序
        boolean swapped = true;
        for (int i = 1; swapped && i < array.length; i++)
        {
            swapped = false;
            for (int j = 0; j < array.length - i; j++)
            {
                if (array[j].compareTo(array[j + 1]) > 0)
                {
                    T temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }
        }
    }

    public static <T> void bubbleSort(T[] array,Comparator<T> comp)
    {
        // 通用冒泡排序
        boolean swapped = true;
        for (int i = 1; swapped && i < array.length; i++)
        {
            swapped = false;
            for (int j = 0; j < array.length - i; j++)
            {
                if (comp.compare(array[j], array[j+1])>0)
                {
                    T temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }
        }

    }

    public static void main(String[] args)
    {
        Double[] aa =
        { 1.0, 34., 5., 7., 3., 98., 54. };
        Student[] stu =
        { 
                new Student("汪汪汪", 17), 
                new Student("喵喵喵", 45), 
                new Student("王焕琪", 23)
        };

        bubbleSort(stu);
        System.out.println(Arrays.toString(stu));
        
//      bubbleSort(stu,new Comparator<Student>()
//      {
//
//          @Override
//          public int compare(Student o1, Student o2)
//          {
//              return o1.getAge()-o2.getAge();
//          }
//      });
        
        //lambda表达式
        bubbleSort(stu, (o1,o2)->{
            return o1.getAge()-o2.getAge();
        });
        System.out.println(Arrays.toString(stu));
        
        bubbleSort(aa);
        System.out.println(Arrays.toString(aa));
    }
}

class Student implements Comparable<Student>
{
    private int age;
    private String name;

    public Student(String name, int age)
    {
        this.age = age;
        this.name = name;

    }

    @Override
    public String toString()
    {
        return name + ":" + age;
    }

    @Override
    public int compareTo(Student o)
    {
        return -this.name.compareTo(o.name);
    }
    public int getAge()
    {
        return age;
    }

}

跑马灯

package Day12_23_00;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.Timer;

@SuppressWarnings("serial")
public class Demo_001 extends JFrame
{
    String string="欢迎来到千锋 ************";
    public Demo_001()
    {
        this.setTitle("");
        this.setSize(300, 100);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        JLabel jLabel=new JLabel(string);
        jLabel.setFont(new Font("微软雅黑",Font.PLAIN,25));
        this.add(jLabel);
        
        Timer timer=new Timer(500, e->{
            string="fadsfa";
            string=string.substring(1)+string.charAt(0);
            jLabel.setText(string);
        });
        timer.start();
    }
    
    public static void main(String[] args)
    {
        new Demo_001().setVisible(true);
    }
}
package Day12_23_00;
/*
 * String是不变字符串,对字符串操作会创建新的字符串对象
 * StringBuffer和StringBuilder代表可变字符串
 * 对字符串修改时,不会创建新的字符串对象
 * StringBuffer是线程安全的-多个线程可以操作同一个StringBuffer对象
 * StringBuilder-是线程不安全的-多个线程同时操作一个StringBuilder时会发生错误
 * StringBuider是java5引入的可变字符串类型 他拥有比StringBuffer更好的性能
 * 
 * 如果要频繁的修改一个字符串请不要使用String,因为每次修改都可能创建新的字符串对象,
 * 所以在这种场景下(时间消耗实验)应该使用StringBuffer或者StringBuilder
 */
public class Test002
{
    public static void main(String[] args)
    {
        StringBuffer sb=new StringBuffer("hello");
        //append()-在字符串末尾追加新的内容
        sb.append("world!");
        sb.append(" goodbye");
        //insert()-从指定位置插入新内容
        sb.insert(5,"good");
        sb.insert(0, "hi!");
        System.out.println(sb);
        
        //deletCharAt()-删除指定位置字符
        sb.deleteCharAt(10);
        sb.delete(0, 9);
        System.out.println(sb);
        
        //reverse()-字符串反转
        sb.reverse();
        System.out.println(sb);
        
        //setCharAt()-修改指定位置的字符
        sb.setCharAt(3, '#');
        System.out.println(sb);
        
    }
}

正则表达式--regular expression

package Day12_23_01;
//正则表达式-regular expression
/*
 * 定义字符串的匹配模式
 */
public class Test003
{
    public static void main(String[] args)
    {
        //验证手机号码是否合法
        String str="12545466772";
        
        boolean isValid= str.matches("1[3478][0-9]{9}");//[0-9]-->\\d
        System.out.println(isValid);
        //验证QQ号是否合法
        String qqid="5345443896786788789";
        boolean isValidQQ=qqid.matches("[1-9]\\d{4,11}");//不设上限{4,}
        System.out.println(isValidQQ);
        
        //验证用户名
        String username="dfafkj345435";
        boolean isValidusernaem=username.matches("[a-zA-Z0-9_]{6,20}");
        //[^a-zA-Z0-9_]可用\\W-->不能是字母数字下划线
        System.out.println(isValidusernaem);
        
        //文字马赛克
        String msg="操你大爷,日你大爷,干你大爷,fuck你大爷";
        msg=msg.replaceAll("[^操干日]|[fF]\\s*[Uu]\\s*[Cc]\\s*[Kk]", "*");
        System.out.println(msg);
        
    }
}

创建正则表达式Pattern

package Day12_23_01;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
//创建正则表达式Pattern
public class Test004
{
    public static void main(String[] args)
    {
        boolean ismatchs=Pattern.matches("\\w{6,10}", "jdfadlfjkad");
        System.out.println(ismatchs);
        
        Pattern pattern=Pattern.compile("\\w{6,20}");
        Matcher matcher=pattern.matcher("jeckfroud");
        System.out.println(matcher.matches());
        
        Pattern pattern2=Pattern.compile("a*b",Pattern.CASE_INSENSITIVE);
        Matcher matcher2=pattern2.matcher("qrqerqwabbaABABbaBaaabaabaaab");
        while (matcher2.find())
        {
            System.out.println(matcher2.group());
        }
    }
}

相关文章

  • Day12_23

    String ,StringBuffer,StringBuilder 跑马灯 正则表达式--regular exp...

网友评论

      本文标题:Day12_23

      本文链接:https://www.haomeiwen.com/subject/timlvttx.html