学习内容总结(week 4)

1.内部类

2."=="与equals的区别

3.String 与 Date 类型转换

4.StringBuffer 与 String 类型转换

5.int 与 String 类型转换

6.byte 与 String 类型转换

7.String 常用方法

8.常见 String 方面应用

9.StringBuffer 的基本方法应用

10.StringBuffer 的特有功能

11.Random 类的使用

12.Math 类的方法应用

13.集合和数组的区别

14.Collection的高级功能

15.Collection 的迭代器

16.增强 for 循环

17.插入排序


1.内部类

1)成员内部类: 内部类处于外部类成员位置
    例:(伪代码,只显示格式)    
        class Outer
            class Inner{
                public void show();
            }
        访问: 
        格式:    外部类名.内部类名 对象名 = 外部类对象.内部类对象;
            Outer.Inner oi = new Outer().new Inner();
                  oi.show();
2)静态成员内部类: 内部类处于外部类成员位置
    例:c
        class Outer
            public int a = 12;
            public static int a2 = 11;
                static class Inner{
                    public void show(){
                        //System.out.println(a); 无法调用,非静态
                        System.out.println(a2); 静态,可调用
                        public static void show2();
                    }
                }
        访问:
        格式: 外部类名.内部类名 对象名 = new 外部类名.内部类名();
            Outer.Inner oi = new Outer.Inner();
            oi show();
            oi show2();//不推荐, show2 为静态方法
            Outer.Inner.show2();//推荐
3)局部内部类: 成员方法内的一个类
    例:(伪代码,只显示格式)
        class Outer{
            public int num = 10;
            public int num2 = 11;
            public void method(){
                class Inner{
                    public void show(){
                        System.out.println(num);
                        System.out.pringln(num2);
                    }
                }
                Inner inner = new Inner();
                    inner.show2();
            }
        }
    访问:
        Outer outer = new Outer();
        outer.method();
4)匿名内部类: 
    格式: new 类名(可以是抽象类,也可以是具体类)或者是接口名(){重写功能}
    例:
        interface Inter {
             void show();
             void show2();
        }
        class Outer{
            public void method(){
                Inter i = new Inter(){
                    public void show(){}
                    public void show2(){}
                    i.show();
                    i.show2();
                }
            }
        }
        访问:
            Outer outer = new Outer();
            outer.method();


2."=="与equals的区别

==: 
        1)连接基本类型,比较的是数据值是否相同;
        2)连接的是引用类型,比较的是地址值是否相同;
        
    equals:
        在底层重写了 Object 的equals 方法,重写之后比较的是成员信息内容.
        
    例:
        public class String Test 
            String s1 = "hello";
            String s2 = "world";
            String s3 = "helloworld";
        输出:
            s3 == (s1+s2);   //false s1+s2 先=开辟空间,在拼接
            s3.equals(s1+s2);// true 
            s3 == "hello"+"world"; // true 
            // 先拼接,然后到常量池中找,有责放回地址
            
            s3.equals("hello"+"world");// true 


3.String 与 Date 类型转换

  1)java.util.Date → String  格式化
      a.创建对象
          Date date = new Date();
      b.创建桥梁 SimpleDateFormat 对象
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      c.格式化操作
          String textDate = sdf . format(date);
      
  2)String(日期文本字符串) → java.util.Date  解析
      a.日期文本字符串 
          String source = "2021-7-30";
      b.    
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      c.解析
          Date date = sdf .parse(source);


4.StringBuffer 与 String 类型转换

  1)String → StringBuffer
          String s = "hello";
      a.使用 StringBuffer 的有参构造方法
          StringBuffer sb = new StringBuffer(s);
      b.或者使用 StringBuffer 的追加功能
          StringBuffer sb = new StringBuffer();
          sb.append(s);
          
  2)StringBuffer → String
      a.String类的构造方法 String(StringBuffer)
          StringBuffer buff = new StringBuffer("world");
              String str = new String(buffer);


5.int 与 String 类型转换

  1)int → String
      //c toString
          public static String toString(int i);
          
  2) String → int
      // integer 类型的静态功能
          public static int parseInt(String s);
          
  3)String → Integer → int
          String s = "100";
          Integer i = new Integer(s);
          int num = i.intValue();


6.byte 与 String 类型转换

 1)解码:
          String str = "中国";
          byte[] bytes = str.getbytes();
        输出: Array.toString(bytes);
        
  2)编码:
          String str = new String(bytes);
          输出: str


7.String 常用方法

1) public String() : 空参构造,空字符序列;
        例:
            String s = new String();
            System.out.println(s);//空
            
    2) public String(byte[] bytes) : 将一个字节数组构造成一个字符串;
        例:    
            byte[] bytes = {97,98,99,100,101};
            String s = new String(bytes);
            System.out.println(s);//A,B,C,D,E,F
            
    3) public String(byte[] bytes,int offset,int length): 将指定部分字节数组转换成字符串. 参数1:字节数组对象;参数2:指定的角标值;参        数3指定长度;
        例:
            byte[] bytes = {97,98,99,100,101};
            String s = new String(bytes,2,2);
            System.out.println(s);//C,D
            
    4)public String(char[] value): 将字符数组构造成一个字符串;
        例:
            char[] ch = {'你','吃','了','吗'};
            String s = new String(ch);
            System.out.println(s)//你吃了吗
            
    5)public String(char[] value,int offset,int count): 将部分字符数组转换成字符串;
        例:
            char[] ch = {'你','吃','了','吗'};
            String s = new String(ch,1,4)
            System.out.println(s);// 吃了吗
    6)public String(String original): 构造一个字符串,参数为字符串常量;
        例:
            String s = new String("love");
            System.out.println(s);// love


8.常见 String 方面应用

 1)键盘录入字符串并反转:
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner s  = new Scanner(System.in) ;
        System.out.println("请输入一个字符串:");
        String str = s.nextLine();
       String result =  reverse(line) ; //调用功能
        System.out.println(result);
    }
    public static String reverse(String s) {
        String result = "" ; //定义一个结果变量
        char[] chs = s.toCharArray(); //将字符串转换成char[]
        for(int x = chs.length-1 ; x>=0 ; x --){
            result += chs[x] ;
        }
        return  result ;
    }
}


2)键盘录入字符串,判断是否对称
import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in) ;
        System.out.println("请输入一个字符串:");
        String line = sc.nextLine() ;        
        boolean flag = compare(line) ;//调用功能
        System.out.println(flag);

    private static boolean compare(String line) {
        char[] charArray = new char[line.length()] ;
        //遍历line字符串,通过charAt(int index)获取每一个字符
        for(int x = 0 ; x < line.length() ; x ++){
         charArray[x] = line.charAt(x) ;//对字符数组每个元素赋值
        }
        //遍历字符数组charArray,保证长度/2
        for(int i = 0 ; i < charArray.length/2 ; i ++){
            if(charArray[i] !=charArray[charArray.length-1-i]) {
                return false;
            }
        }
        return true ;
    }
}


9.StringBuffer 的基本方法应用

 1)添加功能   StringBuffer append/StringBuffer inset(int                                                     offset,String str)
   a. public class StringBufferDemo {
       public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();//创建字符串缓冲区对象
        System.out.println("sb:"+sb);       sb.append("hello").append("world").append(100).append('a').append(12.34).append(new Object()) ;
        System.out.println("sb:"+sb);
        
    b.    sb.insert(5,"吃饭") ;
        System.out.println("sb:"+sb);
    }
}

 
 2)删除功能:    
         a.StringBuffer deleteCharAt(int index)//删除指定索引处的缓冲区的字符序列,返回字符串缓冲区本身;
         b.StringBuffer delete(int start,int end)//删除从指定位置到指定位置结束的字符序列(包含end-1处的字符),返回字符串缓冲区本身
           [start,end-1];
     public class StringBufferDemo {
    public static void main(String[] args) {
        //创建一个StringBuffer对象 (默认初始容量16)
        StringBuffer buffer = new StringBuffer() ;
        buffer.append("hello").append("world").append("Javaee")
      a.    //删除第一个l字符
  System.out.println("deletCharAt():"+buffer.deleteCharAt(1));
      

      b.   //删除world这个子字符串
        System.out.println("delete():"+buffer.delete(5,10));    }
}


10.StringBuffer 的特有功能

 1)反转功能
     import java.util.Scanner;    
     public class StringBufferDemo4 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in) ;
        System.out.println("请您输入一个字符串:");
        String line = sc.nextLine() ;
        //需要用StringBuffer的特有功能 reverse
        //line---->StringBuffer类型
        StringBuffer sb = new StringBuffer(line) ;
        String result = sb.reverse().toString();
        System.out.println(result);
    }
    public static String reverse(String s){
        //方式1:s---->StringBuffer类型---->有参构造
           StringBuffer buffer = new StringBuffer(s) ;
           return buffer.reverse().toString() ;

        //方式2:append()+空参构造方法
      StringBuffer buffer = new StringBuffer() ;
        String result = buffer.append(s).reverse().toString();
        return result ;
        //或者匿名对象
      return new StringBuffer().append(s).reverse().toString() ;
              (以上三种表达方式均可)
    }
}


 2)截取功能
      a. public String substring(int start)//从指定位置开始,默认截取到末尾,返回值是新的字符串;
      b. public String substring(int start,int end)//从指定位置开始到指定end-1结束进行截取,返回的新的字符串.
      
      public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer() ;                    sb.append("hello").append("world").append("javaee").append("anroid") ;  
        System.out.println("sb:"+sb);
        System.out.println(sb.substring(5));//subString(xx)---->截取的新的字符串
        System.out.println("sb:"+sb);
        System.out.println(sb.substring(5,10));//end-1位置
        
        
 3)替换功能
     public StringBuffer replace(int start,       起始索引
                                                int end,         结束索引(end-1)
                                                String str)      替换的内容
                                
    public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer() ;        sb.append("hello").append("world").append("javaee").append("anroid") ;
        System.out.println(sb.replace(5,10,"吃饭"));


11.Random 类的使用

 1) public Random():产生一个随机生成器对象,通过成员方法随机数每次不一样
  2) public int nextInt():获取的值的范围是int类型的取值范围(-2的31次方到2的31次方-1);
  3) public int nextInt(int n):获取的0-n之间的数据 (不包含n);
  
  public class RandomDemo {
    public static void main(String[] args) {
        //创建一个随机数生成器
        Random random =  new Random() ;
        //产生10个数
        for(int x = 0 ; x < 10 ; x ++){
            // public int nextInt():
            //int num = random.nextInt();(范围大,可以下述方式控范围)
            //public int nextInt(int n)
            int num =(random.nextInt(30)+1);//产生10个1-30的随机数
            System.out.println(num);
        }
    }
}


12.Math 类的方法应用

1)  public static int abs(int  a):绝对值方法
2)  public static double ceil(double a):向上取整
3)  public static double floor(double a):向下取整
4)  public static int max(int a,int b):获取最大值
5)  public static int min(int a,int b):获取最小值
6)  public static double pow(double a,double b):a的b次幂
7)  public static double random():[0.0,1.0):随机数
8)  public static long round(double a):四舍五入
9)  public static double sqrt(double a):开平方根

public class MathDemo {
    public static void main(String[] args) {
        //abs()
        System.out.println(Math.abs(-1));
        // public static double ceil(double a):向上取整
        System.out.println(Math.ceil(9.22));
       // public static double floor(double a):向下取整
        System.out.println(Math.floor(10.51));
        // public static int max(int a,int b):获取最大值
        System.out.println(Math.max(Math.max(2,5),6));
        //public static double pow(double a,double b)a的b次幂
        System.out.println(Math.pow(3,4));
        //ublic static long round(double a):四舍五入
        System.out.println(Math.round(4.56));
        //  public static double sqrt(double a):开平方根
        System.out.println(Math.sqrt(9));
    }
}


13.集合和数组的区别

1) 长度的区别
    数组: 长度固定;
    集合:    长度可变;
2) 存储数据类型的区别
    数组:    可以存储基本数据类型,也可存储引用数据类型;
    集合: 不加入泛型可存储任意类型数据,加入泛型只能存储引用数据类型;


14.Collection的高级功能

 1) boolean addAll(Collection c):添加一个集合中的所有元素;
 
 2) boolean containsAll(Collection c):包含一个集合中的所有元素;
 
 3) boolean removeAll(Collection c):删除集合中的所有元素,删除一个算删除;

 4) boolean retainAll(Collection c):A集合堆B集合求交集,交集的元素存储在A集合中,;返回值: 看A集合的元素是否有变化(之前的元素和现在交集的元素进行对比)如果有变化,返回true;没有变化,则返回false.


15.Collection 的迭代器

  Collection 的专有遍历方式 Iterator 接口
   Iterator iterator():返回值类型接口类型,需要返回的子实现类对象;
    boolean hasNext():判断迭代器中是否存在下一个元素;
    Object next():  获取下一个可以遍历的元素;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;(导包)
    public class CollectionTest {
    public static void main(String[] args) {
        //创建集合对象
        Collection c = new ArrayList() ; //List接口的子实现类 (重复元素)
        //添加元素
        c.add("hello") ;
        c.add("world") ;
        c.add("javaee") ;
        //获取Collection的迭代器Iterator iterator()
        Iterator it = c.iterator();
          while(it.hasNext()){//判断迭代器中有下一个元素
            //获取
            Object obj = it.next();// Object obj = new String("hello") ...
            //  System.out.println(obj+"----"+obj.length());
            //获取的元素同时,还要获取当前存储元素的长度  ---->String类型length()
            String str = (String) obj;//向下转型
            System.out.println(str+"----"+str.length());
        }

    }
}


16.增强 for 循环

  格式: 
           for(存储的引用数据类型 变量名: 集合/数组对象)
           
           public class ForeachDemo {

    public static void main(String[] args) {
       //创建List集合
        List<String> list = new ArrayList<>() ;
        list.add("hello") ;
        list.add("world") ;
        list.add("javaee") ;
       if(list!=null){
           for(String s:list){//获取迭代器
               System.out.println(s+"---"+s.length());
           }
       }else{
           System.out.println("当前集合对象为null了");
       }
    }
}


17.插入排序

核心思想:
        使用1角标对应的元素进行和0角标比较,如果前面元素大,向右移动,确定角标1对应的元素的位置,再次使用2角标对应的元素依次和1和0都元素比较.
    
    public class InsertSortTest {
    public static void main(String[] args) {
        //定义一个Integer数组: Integer实现的自然排序:元素能够按照升序默认排序
        Integer[] arr = {34,8,64,51,32,21} ;
        System.out.println("排序前:");
        printArr(arr);
        //定义一个功能
        insertSort(arr) ;
        System.out.println("排序后:");
        printArr(arr);
    }
    //插入排序
    private static void insertSort(Integer[] arr) {
            int j ; //j记录当前角标的变化
                //开始比较
            for(j = p ; j>0 && temp.compareTo(arr[j-1])<0; j-- ){  // j= 1 ; 1>0&& 8 < 34   j-- : j= 0
              //j=2 ; 2>0 && 64 < 34
                  arr[j] = arr[j-1] ;  //数据移动
                }
               //确定temp的位置:8的位置,64的位置:p=2
                arr[j] = temp ;         // 没有移动
        }
    }
    public static void printArr(Integer[] arr){
        System.out.print("[");
        for(int x = 0 ; x < arr.length ; x ++){
            if(x == arr.length -1){
                System.out.println(arr[x] +"]");
            }else{
                System.out.print(arr[x]+", ");
            }
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于心脏病预测的问题,使用循环神经网络(RNN)是一种常见的方法。RNN适用于处理序列数据,而心电图信号就是一种序列数据。在使用RNN进行心脏病预测时,你可以将心电图信号作为输入序列,然后通过训练RNN模型来预测患者是否患有心脏病。 首先,你需要准备一个合适的数据集,其中包含心电图信号和相应的心脏病标签。可以使用公开的心电图数据集,如PTB数据库或MIT-BIH数据库。然后,你可以对数据进行预处理和特征工程,如数据清洗、滤波、降采样等。 接下来,你可以构建一个RNN模型。RNN模型由一系列循环层组成,每个循环层都会处理一个时间步的输入数据。你可以选择不同类型的RNN单元,如简单循环单元(SimpleRNN)、长短期记忆网络(LSTM)或门控循环单元(GRU)。通过添加适当的全连接层和激活函数,你可以将RNN模型输出映射到二分类问题(有或无心脏病)的结果。 然后,你可以使用训练集对RNN模型进行训练,并使用验证集进行模型调优。在训练过程中,你可以使用适当的损失函数(如交叉熵)和优化算法(如随机梯度下降)来最小化模型的预测误差。 最后,你可以使用测试集对训练好的模型进行评估,并计算模型的性能指标,如准确率、精确率、召回率等。这些指标可以帮助你评估模型的预测能力和泛化能力。 需要注意的是,心脏病预测是一个复杂的医学问题,仅仅使用心电图信号可能不能得到准确的预测结果。通常情况下,还需要结合其他患者的临床信息和医学知识来进行综合评估。因此,在进行心脏病预测时,建议与专业医生合作,并遵循相关的医学准则和规范。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值