• 关于JavaSE程序的小总结(不分先后顺序 后续继续补充)


    统计字符串中某个字符串出现的次数

    package com.jiang.demo01;
    
    public class Demo01 {
        public static void main(String[] args) {
            String str = "abdfjavaadadjavaadasjavasdfdsfjavadsfdsjavasdf";
            int length = str.length();
            //获取字符串母串的长度
            String java = str.replace("java", "");
            //把java用空字符串代替,这样就可以消去java,留下剩下的子串
            int length1 = java.length();
            //获取子串的长度,用母串的长度减去子串的长度除以java的长度4就是它出现的次数
            //这个除数由我们要截的字符串长度决定
      System.out.println("java出现了"+(length-length1)/4+"次");
    
    
        }
    }
    
    

    字节数组复制MP3

    public class MyTest {
        public static void main(String[] args) throws IOException {
            FileInputStream fis = new FileInputStream("E:\断了的弦.mp3");
            FileOutputStream fos = new FileOutputStream("D:\断了的弦.mp3");
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len=fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
             fos.close();
            fis.close();
    
        }
    }
    

    字节流复制文本文件

    public class MyTest {
        public static void main(String[] args) throws IOException {
            FileInputStream fis = new FileInputStream("E:\a.txt");
            FileOutputStream fos = new FileOutputStream("D:\a.txt");
            int len = 0;
            while ((len=fis.read())!=-1){
              
                fos.write(len);//写入文件
            }
             fos.close();
            fis.close()
            ;
        }
    }
    

    数组冒泡排序

    public class Demo02 {
        public static void main(String[] args) {
            int [] arr={10,20,30,40,234,4234,23432,566,-121};
            for (int j = 0; j < arr.length-1; j++) {
                for (int i = 0; i < arr.length-1; i++) {
                //遍历数组,如果前面比后面大就往后放
                    if (arr[i]>arr[i+1]){
                        int t=arr[i];//使用第三个变量来替换两个值
                        arr[i]=arr[i+1];
                        arr[i+1]=t;
           
                    }
    
                }
            }
            System.out.println(Arrays.toString(arr));
        }
    }
    

    数组选择排序

    public class Demo02 {
        public static void main(String[] args) {
            int [] arr={10,20,30,40,234,4234,23432,566,-121};
            for (int index = 0; index < arr.length-1; index++) {
                for (int i = 1+index; i < arr.length; i++) {
               //1+index:自己跟自己不需要比,所以每次遍历往后推一个,就加上外部index循环的值
                    if (arr[index]>arr[i]){//如果前面的值比后面的值大,就把大的一直往就挪
                        int t=arr[index];//使用第三个变量来替换两个值
                        arr[index]=arr[i];
                        arr[i]=t;
                    }
                }
            }
    
            System.out.println(Arrays.toString(arr));
        }
    }
    
    
  • 相关阅读:
    【WP开发】记录屏幕操作
    【.NET深呼吸】清理对象引用,有一个问题容易被忽略
    【WP开发】JSON数据的读与写
    【WP8.1开发】RenderTargetBitmap类的特殊用途
    【WP 8.1开发】How to 图像处理
    【WP8.1开发】用手机来控制电脑的多媒体播放
    【WP 8.1开发】如何动态生成Gif动画
    【WP8.1开发】基于应用的联系人存储
    使用awk处理文本
    PHP数组和字符串的处理函数汇总
  • 原文地址:https://www.cnblogs.com/godles/p/11869163.html
Copyright © 2020-2023  润新知