String字符串类课后作业

String动手动脑和课后作业

 

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

 

结果:

 

总结:在Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s0s1s2实际上引用的是同一个对象。

编译器在编译s2一句时,会去掉“+”号,直接把两个字串连接起来得一个字串(“Hello”)。这种优化工作由Java编译器自动完成。

当直接使用new关键字创建字符串对象时,虽然值一致(都是“Hello”),但仍然是两个独立的对象。

再看

 

结果:

 

结论:给字串变量赋值意味着:两个变量(s1s2)现在引用同一个字符串对象“a”!

String对象的内容是只读的,使用“+”修改s1变量的值,实际上是得到了一个新的字符串对象,其内容为“ab”,它与原先s1所引用的对象”a”无关,所以,s1==s2返回false

代码中的“ab”字符串是一个常量,它所引用的字符串与s1所引用的“ab”对象无关。

String.equals()方法可以比较两个字符串的内容。

课后作业

古罗马皇帝凯撒在打仗时曾经使用过加密军事情报:请编写一个程序,使用上述算法加密或解密用户输入的英文字串。

 

设计思想

创建一个数组,依次读取输入的字符,输出对应的字符,最后三个字母在数组的后面加上abc与之对应。

程序流程图

源程序: 

 

import java.util.Scanner;

 

public class Jiami{

 

    public static void main(String[] args) {

 

     String yuanwen;

    

     char s1=' ';

    

     System.out.println("请输入一行字符:");

 

     Scanner s = new Scanner(System.in);

 

     yuanwen = s.nextLine();

 

    char cl[]=yuanwen.toCharArray();

 

    char[] c = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c'};

 

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

 

        char[] c1 = new char[yuanwen.length()];

 

        c1[i] = yuanwen.charAt(i);

 

        for(int j = 0; j < 26; j++){

 

            if(c1[i] == c[j]){

 

              s1 = c[j + 3];

 

            }

           

        }

       

      System.out.print( s1);

 

    }

 

  }

   

}

 

结果截图:

 

转载于:https://www.cnblogs.com/liulitianxia/p/6076702.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己实现的字符串 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值