为了账号安全,请及时绑定邮箱和手机立即绑定
慕课网数字资源数据库体验端
Java入门第三季_学习笔记_慕课网
为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第三季

陈码农
难度入门
时长 5小时 0分
  • String类的用法

    查看全部
  • Collection接口、子接口以及实现类

    Collection接口

    1>是List、Set和Queue接口的父接口

    2>定义了可用于操作List、Set和Queue的方法——增删查改


    List接口及其实现类—ArrayList

    1>List是元素有序并且可以重复的集合,被称为序列

    2>List可以精确的控制每个元素的插入位置,或删除某个位置元素

    3>ArrayList—数组序列,是List的一个重要实现类

    4>ArrayList底层是由数组实现的


    查看全部
  • java集合框架的大致结构

    查看全部
  • 泛型集合中不允许使用基本数据类型,如果一定要使用,只能使用基本类型的包装类,如integer,Long,Character,Short

    查看全部
  • List的增删改查:

    增 :使用 add和addAll两种方法

    add(新的元素)和addAll(新的数组元素)PS:新的数组元素要用Arrays.asList();把数组转换成集合

    删:使用remove(元素位置)和removeALL(数组位置),来删除集合中的元素

    改:使用set(元素位置,新new的元素) 来改变原本的元素

    查:使用 for循环,iterator迭代器,for each来遍历和查询List集合中的元素

    PS:其中要使用到 集合.size();来获取List中的元素长度。

    PS:  使用 iterator迭代器时,可以使用while(Iterator.XX.hasNext())来判断是否遍历了集合中的所有元素有效位,并配合 XX.next(),来循环打印每一个集合中的元素,//这么说可能只有我自己能看懂,各位自求多福

    查看全部
  • 字节是计算机存储信息的基本单位,1 个字节等于 8 位, gbk 编码中 1 个汉字字符存储需要 2 个字节1 个英文字符存储需要 1 个字节。所以我们看到上面的程序运行结果中,每个汉字对应两个字节值,如“学”对应 “-47 -89” ,而英文字母 “J” 对应 “74” 。同时,我们还发现汉字对应的字节值为负数,原因在于每个字节是 8 位,最大值不能超过 127,而汉字转换为字节后超过 127,如果超过就会溢出,以负数的形式显示

    查看全部
  • 每次 new 一个字符串就是产生一个新的对象,即便两个字符串的内容相同,使用 ”==” 比较时也为 ”false” ,如果只需比较内容是否相同,应使用 ”equals()” 方法

    查看全部
  • 一旦一个字符串在内存中创建,则这个字符串将不可改变。如果需要一个可以改变的字符串,我们可以使用StringBuffer或者StringBuilder(后面章节中会讲到)。

    查看全部
  • 一、String类
    String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。
    二、String类对象的创建
    字符串声明:

    1String stringName;

    字符串创建:

    1String stringName = new String(字符串常量);或String stringName = 字符串常量;


    三、String类构造方法
    1、public String()
    无参构造方法,用来创建空字符串的String对象。

    1String str1 = new String();

    2、public String(String value)
    用已知的字符串value创建一个String对象。

    12String str2 = new String("asdf");String str3 = new String(str2);

    3、public String(char[] value)
    用字符数组value创建一个String对象。

    12char[] value = {'a','b','c','d'};String str4 = new String(value); //相当于String str4 = new String("abcd");

    4、public String(char chars[], int startIndex, int numChars)
    用字符数组chars的startIndex开始的numChars个字符创建一个String对象。

    12char[] value = {'a','b','c','d'};String str5 = new String(value,1,2); //相当于String str5 = new String("bc");

    5、public String(byte[] values)

    用比特数组values创建一个String对象。

    12byte[] strb = new byte[]{65,66};String str6 = new String(strb); //相当于String str6 = new String("AB");

    四、String类常用方法
    1、求字符串长度

    123public int length()//返回该字符串的长度String str = new String("asdfzxc");int strlength = str.length(); //strlength = 7

    2、求字符串某一位置字符

    1234public char charAt(int index);//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1String str = new String("asdfzxc");char ch = str.charAt(4);//ch = z

    3、提取子串
    用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:

    12345public String substring(int beginIndex); //该方法从beginIndex位置起,从当前字符串中取出剩余的字符串作为一个新的字符串返回。 public String substring(int beginIndex,int endIndex);//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符串作为一个新的字符串返回。
    123String str1 = new String("asdfzxc");String str2 = str1.substring(2); //str2 = "dfzxc";String str3 = str1.substring(2,5); //str3 = "dfz";

    4、字符串比较

    12public int compareTo(String anotherString); //该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0
    1public int compareTolgnore(String anotherString);//与compareTo方法相似,但忽略大小写
    12public boolean equals(Object anotherObject); //比较当前字符串和参数字符串,在两个字符串相等的时候返true,否则返回false
    1public boolean equalslgnoreCase(String anotherString); //与equals方法相似,但忽略大小写
    123456String str1 = new String("abc");String str2 = new String("ABC");int a = str1.compareTo(str2); //a> 0int b = str1.compareTolgnore(str2); //b=0boolean c = str1.equals(str2); //c = false;boolean d= str1.equalslgnoreCase(str2); //d = true

    5、字符串连接

    123public String concat(String str); //将参数中的字符串str连接到当前字符串的后面,效果等价于“+”String str = "aa".concat("bb").concat("cc");String str = "aa"+"bb"+"cc";

    6、字符串中单个字符查找

    12public int indexOf(char ch/String str);//用于查找当前字符串中字符或子串,返回字符或者子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1
    12public int indexOf(char ch/String str,int formIndex); //该方法与第一种类似,区别在于该方法从fromIndex位置向后查找
    12public int lastIndexOf(char ch/String str); //该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找
    12public int lastIndexOf(char ch/String str,int fromIndex);//该方法与第二种方法类似,区别在于该方法从fromIndex位置向前查找
    123456String str = "I am a good student";int a = str.indexOf('a');//a = 2int b = str.indexOf("good"); //b = 7int c = str.indexOf('w',2); //c = -1int d = str.lastIndexOf('a');//d = 5int e = str.lastIndexOf('a',3);// e = 2

    7、字符串中字符的大小写转换

    12public String toLowerCase(); //返回将当前字符串中所有字符转换为小写后的新字符串public String toUpperCase(); //返回将当前字符串中所有字符转换为大写后的新字符串
    123String str = new String("asDF");String str1 = str.toLowerCase();//str1 = "asdf";String str2 = str.toUpperCase(); // str2 = "ASDF";

    8、字符串中字符的替换

    12public String replace(char oldChar,char newChar);//用于字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
    12public String replaceFirst(String regex,String replacement);//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
    12public String replaceAll(String regex,String replacement);//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
    12345String str = "asdzxcasd";String str1 = str.replace('a','g');//str1 = "gsdzxcgsd";String str2 = str.replace("asd","fgh"); //str2 = "fghzxcfgh";String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd";String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh";

    9、其他类方法

    12345String trim();//截去字符串两端的空格,但对于中间的空格不处理String str = " a sd ";String str1 = str.trim(); //str1 = "a sd";int a = str.length();//a = 6;int b = str1.length();//b= 4
    123456boolean statWith(String prefix) 或 boolean endWith(String suffix); //用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。String str = "asdfgh";boolean a = str.statWith("as"); //a = true;boolean b = str.endWith("gh"); //b = true;
    123regionMatches(boolean b,int firstStart,String other,int otherStart,int length)//从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。
    1234contains(String str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。String str = "student";boolean res = str.contains("stu"); //res = true;boolean res1 = str.contains("ok"); //res1 = false;
    123String[] split(String str);//将str作为分隔符进行字符串分解,分解后的子字符串在字符串数组中返回String str = "asd!qwe|zxc#";String str1 = str.split("!|#");// str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";

    五、字符串与基本类型的转换

    1、字符串转换为基本类型

    java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:

    12345678910public static byte parseByte(String s);public static short parseShort(String s);public static int parseInt(String s);public static long parseLong(String s);public static float parseFloat(String s);public static double parseDouble(String s); int n = Integer.parseInt("12");float f = Float.parseFloat("12.34");double d = Double.parseDouble("1.123");
    1<br>

    2、基本类型转换为字符串类型

    String类中提供了String valueOf()放法,用作基本类型转换为字符串类型。

    1234567891011static String valueOf(char data[]);static String valueOf(char data[],int offset,int count)static String valueOf(boolean b)static String valueOf(char c)static String valueOf(int i)static String valueOf(long l)static String valueOf(float f)static String valueOf(double d) String s1 = String.valueOf(12);String s1 = String valueOf(12.34);

    3、进制转换

    使用Long类中的方法得到整数之间的各种进制转换的方法:

    1234Long.toBinaryString(long l)Long.toOctalString(long l)Long.toHexString(long l)Long.toString(long l,int p)//p作为任意进制

    Long.toBinaryString(long l)

    Long.toOctalString(long l)
    Long.toHexString(long l)
    Long.toString(long l, int p)//p作为任意进制


    查看全部
  • 异常处理在实际应用中的总结

    查看全部
    0 采集 收起 来源:经验总结

    2018-05-08

  • https://img1.sycdn.imooc.com//5af154a7000187e013660595.jpg

    A错

    查看全部
    0 采集 收起 来源:练习题

    2018-05-08

  • 何时使用try-catch-finally

    查看全部
  • //以下方法会抛出数组下标越界异常

    // Course cr3 = new Course("3", "test");

    // coursesToSelect.add(3, cr3);       

    解释: list的容量size是2, add(index,加入的课程),其中index可以的值为0,1,2

    当为0,1时,加入的课程为index这个位置,原来的往后挪一个位置,当为2时,原来的课程位置不变,加入的课程在最后一位。


    查看全部
  • list.addAll(集合对象);把一个集合添加到另一个集合的队尾位置
    list.addAll(index,集合对象);把一个集合添加到另一个集合的指定位置
    Arrays.asList(数组):把一个数组转换成List集合

    !!!

    查看全部
  • public class HelloWorld {

        public static void main(String[] args) {

            

            // 定义double类型变量

    double a = 91.5;

            

             // 手动装箱

    Double b =  new Double(a);      

            

            // 自动装箱

    Double c =       a;


            System.out.println("装箱后的结果为:" + b + "和" + c);

            

            // 定义一个Double包装类对象,值为8

    Double d = new Double(87.0);

            

            // 手动拆箱

    double e =    b.doubleValue()           ;

            

            // 自动拆箱

    double f =    c            ;

            

             System.out.println("拆箱后的结果为:" + e + "和" + f);

    }

    }


    查看全部

举报

0/150
提交
取消
课程须知
此部分为 Java 课程的进阶内容,适合具有一定 Java 基础的伙伴们学习,如果您是新手,建议您移步 《Java入门第一季》 和 《Java入门第二季》,在理解并掌握面向对象相关知识后再回来进修。
老师告诉你能学到什么?
本课程将学习 Java 中的异常处理、集合框架、字符串、常用类等,逐步学习掌握 Java 高级技术。
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!