zoukankan      html  css  js  c++  java
  • 动手动脑(字符串)

    1.请运行以下示例代码StringPool.java,查看其输出结果。如何解释这样的输出结果?从中你能总结出什么?

    public class StringPool {

        public static void main(String args[]){

            String s0="Hello";

            String s1="Hello";

            String s2="He"+"llo";

            System.out.println(s0==s1);        

            System.out.println(s0==s2);        

            System.out.println(new String("Hello")==new String("Hello"));     

            }

    }

    结果:

     

    分析:

      编译器在编译s2一句时,会去掉“+”号,直接把两个字串连接起来得一个字(“Hello”)。这种优化工作由Java编译器自动完成。在Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存所以s0,s1,s2实际上引用的是同一个对象在一个内存空间内。当直接使用new关键字创建字符串对象时,虽然值一致(都是“Hello”),但开辟了两个不同的内存空间,所以仍然是两个独立的对象。

    运行下面这段程序:

    public class Text5 {

        public static void main(String args[]){        

            String s1="a";        

            String s2=s1;

            System.out.println(s1==s2);

            s1+="b";

            System.out.println(s1==s2);

            System.out.println(s1=="ab");

            System.out.println(s1.equals("ab"));

            }

    }

    结果:

    分析:

    给字串变量赋值意味两个变量s1,s2现在引用同一个字符串对象“a”,所以在修改s1之前,s1与s2是同一个对象,所以输出结果为true;String对象的内容是只读的,使用“+”修改s1变量的值,实际上是得到了一个新的字符串对象,其内容为“ab”,它与原来s1所引用的对象”a”无关,所以s1==s2返回值为false;代码中的“ab”字符串是一个常量,它所引用的字符串与s1所引用的“ab”对象无关,所以结果为false;String.equals()方法可以比较两个字符串的内容,内容都为“ab”,所以返回值为true。

    2.请查看String.equals()方法的实现代码,注意学习其实现方法。

    public boolean equals(Object anObject) {

        if (this == anObject)

        {

            return true;

        }

        if (anObject instanceof String)

        {

            String anotherString = (String) anObject;

            int n = value.length;

        

            if (n == anotherString.value.length)

            {

                char v1[] = value;

                char v2[] = anotherString.value;

                int i = 0;

                while (n-- != 0)

               {

                   if (v1[i] != v2[i])

                       return false;

                   i++;

               }

               return true;

             }

         }

         return false;

     }

    分析:

    String类中的equals首先比较地址,如果是同一个对象的引用,可知对象相等,返回true;如果不是同一个对象,equals方法挨个比较两个字符串对象内的字符,只有完全相等才返回true,否则返回false。

    3.古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报:

     

    请编写一个程序,使用上述算法加密或解密用户输入的英文字串要求设计思想、程序流程图、源代码、结果截图。

    (1)设计思想:

    首先输入一个字符串,在加密的情况下,前23个小写字母或大写字母,则将其加3;后3个小写字母或大写字母,则将其减23。在解密的情况下,前三个小写或者大写字母要加23,其他23个字母要减3。字符串中的每一个字母可以用charAt()函数来找到,判断哪个字母可以用ASC码值实现。

    (2)程序流程图:

    (3)源代码:

    //加密、解密

    //Sun Qiwen,2016.10.27

    import java.util.Scanner;

    public class Caesar {

        public static void main(String args[]){   

            Scanner scanner=new Scanner(System.in);

            System.out.println("请输入要执行的操作: 1.解密   2.加密");

            int n=scanner.nextInt();

            if(n==1)

            {

            System.out.print("请输入要加密的字符串:");

            String string=scanner.next();

            String result ="";

            char stringadd;

            for(int i=0;i<string.length();i++)

            {

                if(string.charAt(i)>87&&string.charAt(i)<91||string.charAt(i)>119&&string.charAt(i)<123)

                {

                 stringadd=(char)(string.charAt(i)-23);

                }

                else 

                if(string.charAt(i)>64&&string.charAt(i)<88||string.charAt(i)>96&&string.charAt(i)<120)

                {

                 stringadd=(char)(string.charAt(i)+3);

                }

                else

                {

                 System.out.print("输入错误!");

                 break;

                }

                result = result + stringadd;

            }

            System.out.print("加密结果为: "+result);

            }

            else if(n==2)

            {

                System.out.print("请输入要解密的字符串:");

                String string=scanner.next();

                String result="";

                char stringsub;

                for(int i=0;i<string.length();i++)

                {

                    if(string.charAt(i)>64&&string.charAt(i)<68||string.charAt(i)>96&&string.charAt(i)<100)

                    {

                     stringsub=(char)(string.charAt(i)+23);

                    }               

                    else 

                    if(string.charAt(i)>67&&string.charAt(i)<91||string.charAt(i)>99&&string.charAt(i)<123)

                    {

                     stringsub=(char)(string.charAt(i)-3);

                    }

                    else

                    {

                     System.out.print("输入错误!");break;

                    }

                    result=result+stringsub;

                }

                System.out.print("解密结果为: "+result);

            }

            else

            {

             System.out.print("请输入12确认操作!");

            }

        }

    }

    (4)结果截图:

    4.整理String类的Length()、charAt()、 getChars()、replace()、toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明。

    (1)Length():获取字符串长度。

     例如:String str=abcd;

          str.Length()=4;

    (2)charAt():获取指定位置的字符。

     例如:str.charAt(0)检索字符串str 中的第一个字符, 

           str.charAt(str.length()-1)检索字符串str最后一个字符。

    3)getChars():获取从指定位置起的字符串复制到字符数组中。

         四个参数:1)拷贝起始的位置,2)拷贝结束的位置,字符串数值

                       3)目标字符串数组,4)为目标字符串数组的起始位置

     例如:char[]s1={‘i’,’’, ’l’,’o’,’v’,’e’,’’,’y’,’o’,’u’};

           String s2 = new String (“you”);

           s2.getChar(0,4,s1,7);

           System.out.println(s1);

           结果为:i love you

     (4)replace():字符串替换。

     通过String类的replace()方法,可以将原字符串中的某个字符替换为指定的字符,得到一个新的字符串,该方法的具体定义如下:  

     public String replace(char oldChar,char newChar)。

    5)toUpperCase()、 toLowerCase():英文大小写转换。

    6)trim():去除头尾空格。

    7)toCharArray():将字符串对象转换为字符数组。

  • 相关阅读:
    时间单位的档案
    Linux中表示“时间”的结构体和相关函数
    linux 下如何给用户添加权限
    C++中虚函数的作用浅析
    C语言中strstr函数
    测试环境下将centos6.8升级到centos7的操作记录(转)
    CURL常用命令(转)
    Centos7管理selinux及使用firewalld管理防火墙
    UML类图几种关系的总结
    linux下面覆盖文件,如何实现直接覆盖,不提示
  • 原文地址:https://www.cnblogs.com/sunqw/p/6007454.html
Copyright © 2011-2022 走看看