zoukankan      html  css  js  c++  java
  • java学习class6(1)

    一.“级联”调用

    1.程序源代码

     1 package class6;
     2 
     3 public class MyCounter {
     4     
     5     private int password = -1;
     6     
     7     
     8     //调用本类(1)
     9     public MyCounter transfer1()
    10     {
    11         MyCounter test = new MyCounter();
    12         test.password = 1;
    13         System.out.println("调用一执行了"+"将password变成"+test.password);
    14         return test;
    15         
    16     }
    17     
    18     //调用本类(2)
    19     public MyCounter transfer2(int password)
    20     {
    21         this.password = password;
    22         System.out.println("调用二执行了"+"将password变成"+this.password);
    23         return this;
    24     }
    25 
    26     public static void main(String[] args) {
    27         
    28         MyCounter test = new MyCounter();
    29         System.out.println("密码的初始值是:"+test.password);
    30         test.transfer1().transfer2(2).transfer2(3);
    31     }
    32 
    33 }

    2.结果截图

    二.String类的常见函数

    1.Length():用一个String类的对象来调用,返回这个字符串的长度.

    eg.String str;

    int length = str.Length();//此时的length就是该字符串的长度

    2.charAt():返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引,序列的第一个char值是在索引为0,索引1,依此类推。

    声明:public void charAt(index);其中index指的是索引值.

    eg.

     1 package com.yiibai;
     2 
     3 import java.lang.*;
     4 
     5 public class StringDemo {
     6 
     7   public static void main(String[] args) {
     8   
     9     String str = "This is yiibai";
    10                    
    11     // prints character at 1st location
    12     System.out.println(str.charAt(0));
    13           
    14     // prints character at 5th location i.e white-space character
    15     System.out.println(str.charAt(4));
    16           
    17     // prints character at 18th location 
    18     System.out.println(str.charAt(17));
    19   }
    20 } 

    3. getChars():截取字符串中指定长度,指定起始位置的一段字符
    声明:void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)

    sourceStart指定了子串开始字符的下标,sourceEnd指定了子串结束后的下一个字符的下标。

    因此,子串包含从sourceStart到sourceEnd-1的字符。接收字符的数组由target指定,target中开始复制子串的下标值是targetStart。

    eg.

     1 String str = "abcdefghikl"; 2 Char[] ch = new char[8]; 3 str.getChars(2,5,ch,0); 

     

     4.replace():方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。

    声明:StringObject.replace(regexp/substr,replacement)

    regexp/substr规定了子字符串或要替换的模式的 RegExp 对象。如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。

    replacement规定了替换文本或生成替换文本的函数。

    eg.

    1 String s = "I Love Java!";
    2 String r = "you";
    3 s = s.replace("I",r);
    4 System.out.println(s);

    5. toUpperCase():方法用于把字符串转换为大写。

    eg.

    String s ="abcd";
    System.out.printfln("未修改前的字符串是:"+s);
    s = s.toUpperCase();
    System.out.printfln("修改后的字符串是:"+s);

    6. toLowerCase(): toUpperCase():方法用于把字符串转换为小写。

    eg.

    String s ="ABCD";
    System.out.printfln("未修改前的字符串是:"+s);
    s = s.toUpperCase();
    System.out.printfln("修改后的字符串是:"+s);

    7.trim():去掉字符串首尾的空格。

    eg.

     1     public static void main(String arg[])
     2     {  
     3       
     4             String a=" I Love Java            ";  
     5       
     6             String b="I Love Java";  
     7       
     8             System.out.println(b.equals(a));  //false
     9       
    10             a=a.trim();//去掉字符串首尾的空格  
    11       
    12             System.out.println(a.equals(b));  //true
    13      }      

    8.toCharArray():该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符

     eg.

     1  public class class6_3
     2 
     3  {
     4          public static void main(String args[])
     5         {
     6                 String str="I Love Java";
     7 
     8                 char[] c=str.toCharArray();
     9 
    10                 System.out.println("数组c的长度为:"+c.length);//11
    11 
    12                 System.out.println(c);//I Love Java
    13 
    14         }
    15 
    16 }                                           

    三.String.equals()方法源码

     1 1.* @param   obj   the reference object with which to compare.  
     2 2.    * @return  {@code trueif this object is the same as the obj  
     3 3.    *          argument; {@code false} otherwise.  
     4 4.    * @see     #hashCode()  
     5 5.    * @see     java.util.HashMap  
     6 6.    */  
     7 7.   public boolean equals(Object obj) {  
     8 8.       return (this == obj);  
     9 9.   }  
    10 1./** 
    11 2.     * Compares this string to the specified object.  The result is {@code 
    12 3.     * true} if and only if the argument is not {@code null} and is a {@code 
    13 4.     * String} object that represents the same sequence of characters as this 
    14 5.     * object. 
    15 6.     * 
    16 7.     * @param  anObject 
    17 8.     *         The object to compare this {@code String} against 
    18 9.     * 
    19 10.     * @return  {@code true} if the given object represents a {@code String} 
    20 11.     *          equivalent to this string, {@code false} otherwise 
    21 12.     * 
    22 13.     * @see  #compareTo(String) 
    23 14.     * @see  #equalsIgnoreCase(String) 
    24 15.     */  
    25 16.    public boolean equals(Object anObject) {  
    26 17.        if (this == anObject) {  
    27 18.            return true;  
    28 19.        }  
    29 20.        if (anObject instanceof String) {  
    30 21.            String anotherString = (String) anObject;  
    31 22.            int n = value.length;  
    32 23.            if (n == anotherString.value.length) {  
    33 24.                char v1[] = value;  
    34 25.                char v2[] = anotherString.value;  
    35 26.                int i = 0;  
    36 27.                while (n-- != 0) {  
    37 28.                    if (v1[i] != v2[i])  
    38 29.                            return false;  
    39 30.                    i++;  
    40 31.                }  
    41 32.                return true;  
    42 33.            }  
    43 34.        }  
    44 35.        return false;  
    45 36.    }  
  • 相关阅读:
    在Ubuntu18.04.2LTS上安装搜狗输入法
    生活点滴:java基础知识细化
    乘风破浪:LeetCode真题_041_First Missing Positive
    乘风破浪:LeetCode真题_040_Combination Sum II
    乘风破浪:LeetCode真题_039_Combination Sum
    乘风破浪:LeetCode真题_038_Count and Say
    乘风破浪:LeetCode真题_037_Sudoku Solver
    乘风破浪:LeetCode真题_036_Valid Sudoku
    乘风破浪:LeetCode真题_035_Search Insert Position
    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
  • 原文地址:https://www.cnblogs.com/tianxiayoujiu/p/7741736.html
Copyright © 2011-2022 走看看