java
诚宝
这个作者很懒,什么都没留下…
展开
-
从字符串中提取所有数字,并且对数字进行排序
public static void main(String[] args) { String str= "sdjfaeifhv34857"; //提取数字 String str1 = str.replaceAll("\\D", ""); System.out.println(str1); //将字符串转换为字符数组 char[] chs = str1.toCharArr...原创 2019-03-14 17:03:38 · 2851 阅读 · 0 评论 -
输入一个字符串,统计字符重 复出现的次数
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); System.out.println("请输入一个字符串"); //创建一个布尔数组,用于标记 boolean[] bs = new boolean[s.length(...原创 2019-03-12 21:48:33 · 705 阅读 · 0 评论 -
输入一个字符串,统计字符串 中出现的数字之和
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); char[] arr = str.toCharArray(); int sum = 0; for(char c :arr){ if(c >='0' &a...原创 2019-03-12 21:19:39 · 2608 阅读 · 1 评论 -
输入一个字符串以及两个数字,按照输入的数字对字符串进行截取。
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); char[] arr = str.toCharArray(); int end = sc.nextInt(); int begin = sc.nextInt();...原创 2019-03-12 21:14:11 · 773 阅读 · 0 评论 -
让用户输入一个字符串,统计字符串中大写字母、小写字母、数字、其他字符的个数。
public static void main(String[] args) {// 1.Scanner类获取用户输入的字符串 Scanner sc = new Scanner(System.in); String str = sc.nextLine(); // 2.创建4个变量,分别记录不同字符类型出现的个数 int count = 0; int bigCount = 0; in...原创 2019-03-12 21:04:18 · 27671 阅读 · 0 评论 -
拼接元素,区分String类+拼接字符串和 StringBuilder的append拼接的运行速度
public static void main(String[] args) { //定义一个数组 String s = ""; //开始计时 long begin = System.currentTimeMillis(); //使用for循环拼接数组 for(int i = 0; i<100000;i++){ s += "a"; } //结束时间 long end =..原创 2019-03-12 20:12:50 · 381 阅读 · 0 评论 -
用java重写equals方法
public static void main(String[] args) {// 重写equals方法Person p = new Person();p.name = “迪丽热巴”;p.age= 20;Person p1 = new Person();p1.name= “古力娜扎”;p1.age=19;System.out.println(p.equals(p1));}}...原创 2019-03-12 19:44:52 · 263 阅读 · 2 评论 -
序列化与反序列化
Student stu = new Student(); stu.setName(""); stu.setAge(10); //序列化 对象--》字节 ObjectOutputStream oos = //装饰设计模式 new ObjectOutputStream(new FileOutputStream("p.data")); //将stu对象转换为字节 oos.writ...原创 2019-03-20 21:51:33 · 108 阅读 · 0 评论 -
将多个文件进行合并
Vector ver = new Vector(); //创建多个流 FileInputStream f1 = new FileInputStream("D:\\a.txt"); FileInputStream f2 = new FileInputStream("D:\\b.txt"); FileInputStream f3 = new FileInputStream("D:\\c.txt...原创 2019-03-20 21:32:59 · 263 阅读 · 0 评论 -
使用字节输入输出流进行拷贝
public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("D:\\Windows10禁用更新.bat"); FileOutputStream fos = new FileOutputStream("D:\\2.bat"); //先读取图片中的...原创 2019-03-20 20:47:24 · 296 阅读 · 0 评论 -
统计java代码的行数
// 定义变量,用于统计换行次数static int count = 0;public static void main(String[] args) throws Exception { //练习:统计java代码的行数 File file = new File("C:\\Users\\Administrator\\workspace"); count(file); Sy...原创 2019-03-20 20:21:51 · 460 阅读 · 0 评论 -
拷贝文件
先去读取目标文件,将读取的内容写入到新的文件中 FileReader fr = new FileReader("D:\\a.txt"); FileWriter fw = new FileWriter("D:\\b.txt"); //创建一个字符数组作为缓冲区 char[] chs = new char[1024]; //定义每次读取的长度 int len; //循环读...原创 2019-03-20 20:06:51 · 131 阅读 · 0 评论 -
输入一个字符串,获取每个字符出现的次数
Scanner sc = new Scanner(System.in); String str = sc.nextLine(); while(str.length() != 0){ //获取原字符串的长度 int oldLength = str.length(); //获取字符串的第一个字符 char c = str.charAt(0); //替换str中所...原创 2019-03-14 17:12:45 · 596 阅读 · 0 评论 -
使用冒泡排序,对字符串进行升序排序
String[] strs = {"nua","sc","avb","adw","rt","gfe"}; //使用冒泡排序 对字符串进行升序排序 for(int i = 0; i < strs.length; i++){ for(int j = 0; j < strs.length-i; j++){ if(strs[j-1].compare原创 2019-03-12 21:58:05 · 3556 阅读 · 0 评论