zoukankan      html  css  js  c++  java
  • 2020-02-28(观看视频笔记)

    1、Scanner:用于接收键盘录入的数据。

      (1)System类下有一个静态的字段:

        public static final InputStream in;  //标准的输入流,对应着键盘录入。

        例1:

          InputStream is = System.in;

      (2)构造方法:

        Scanner(InputStream source);

      (3)基本格式:

        public boolean hasNextXxx():判断是否是某种类型的元素

        public Xxx nextXxx():获取该元素

        例2:

          Scanner sc = new Scanner(System.in);

          if (sc.hasNextInt()) {

            int x = sc.nextInt();

            System.out.print;n(x);

          } else {

            System.out.println("您输入的数据有误!");

          }

      (4)例3:

          Scanner sc = new Scanner(System.in);

          int x = sc.nextInt();

          String str = sc.nextLine();

          System.out.println("x:" + x + " str:" + str);

          输入  10   后,直接输入   x:10 str:

        出现问题了:

          先获取一个数值,再获取一个字符串,就会出现问题。

          主要原因:就是那个换换行符号的问题。

        如何解决?

          A:先获取一个数值后,再创建一个新的键盘录入对象获取字符串。(两个键盘录入对象)

          B:把所有的数据都先按照字符串获取,然后要什么,你就对应转换成什么。

    2、String

      (1)字符串:就是由多个字符组成的一串数据,也可以看成时一个字符数组。

          通过查看API,我们可以知道

          A:字符串字面值“abc”也可以看成一个字符串对象。

          B:字符串是常量,一旦被赋值,就不能被改变。

      (2)构造方法:

          public String():无参构造

          public String(byte[] bytes):把字节数组转成字符串

          public String(byte[] bytes, int offset, int length):把字节数组中从offset开始长度为length的转成字符串

          public String(char[] value):把字符数组转成字符串

          public String(char[] value, int offset, int count):把字符数组中从offset开始长度为length的转成字符串

          public String(String original):把字符串常量值转成字符串

      (3)字符串特点:

          A:字符串直接赋值的方式是先到字符串常量池中去找,如果由就直接返回,没有,则创建并返回。

          B:一旦被赋值就不能被改变。

      (4)面试题:String s = new String("hello"); 和 String s = "hello";的区别? 

          有,前者创建两个对象,后者创建一个。    

       (5)面试题:看程序写结果

          String s1 = "hello";

          String s2 = "world";

          String s3 = "helloworld";

          System.out.println(s3 == s1 + s2);        //false

          System.out.println(s3.equals((s1 + s2)));     //true

          System.out.println(s3 = "hello" + "world");      //true

          System.out.println(s3.equals("hello" + "world"));    //true

        解析:

          A:字符串如果是变量相加,先开空间,再拼接

          B:字符串如果是常量相加,是先加,然后再常量池中找,如果有就直接返回,否则,就创建。

      (6)String的判断功能:

          public boolean equals(Object obj):比较字符串的内容是否相同,区分大小写

          public boolean equalsIgnoreCase(Object obj):比较字符串的内容是否相同,忽略大小写

          public boolean contains(String str):判断主串中是否包含子串

          public boolean startsWith(String str):判断字符串是否以某个指定字符串开头

          public boolean endWith(String str):判断字符串是否以某个指定字符串结尾

          public boolean isEmpty():判断字符串对象是否为空

          注意:

            字符串的内容为空和字符串对象为空不同。

            内容为空:String s = "";

            对象为空:String s = null;

      (7)String的获取功能:

          public int length():获取字符串的长度

          public charAt(int index):获取指定索引位置的字符

          public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引

            为什么这里是int类型,而不是char类型?

            原因是:'a' 和 97 其实都可以代表 'a'。

          public int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引

          public int indexOf(int ch, int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引

          public int indexOf(String str, int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引

          public String substring(int start):从指定位置开始截取字符串,默认到结尾,包含start位置的字符

          public String substring(int start, int end):从指定位置开始到指定位置结束截取字符串,包含start位置的字符,不包含end位置的字符

      (8)String的转换功能:

          public byte[] getBytes():把字符串转换为字节数组

          public char[] toCharArray():把字符串转换为字符数组

          public static String valueOf(char[] chs):把字符数组转换成字符串。

          public static String valueOf(int i):把int类型的数据转换成字符串

             注意:String类的 valueOf 方法可以把任意类型的数据转换成字符串 

          public String toLowerCase():把字符串转成小写

          public String toUpperCase():把字符串转成大写

          public String concat(String str):把字符串拼接

      (9)String类的其他功能:

          替换功能:

            public String replace(char old, char new)

            public String replace(String old, String new)

          去除字符串两端空格:

            pubilc String trim()

          按字典顺序比较两个字符串:

            public int compareTo(String str)

            public compareToIgnoreCase(String str)

       

  • 相关阅读:
    WEB应用图片的格式,以及各自的特点和优化(一) by FungLeo
    Android---App Widget(五)
    【转】VS2010/MFC编程入门之八(对话框:创建对话框类和添加控件变量)
    【转】MFC中用CFile读取和写入文件2
    【转】VC中获取文件的相对路径和绝对路径
    【转】VC中对文件的读写
    【转】VS2010/MFC编程入门之二十五(常用控件:组合框控件Combo Box)
    【转】CString类型互转 int
    【转】Visual Studio快捷键汇总
    【转】Ubuntu下配置支持Windows访问的samba共享
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/12377743.html
Copyright © 2011-2022 走看看