java作业04(动手动脑)

1.请查看String.equals()方法的实现代码,注意学习其实现方法。equals所在位置:

Object类当中,而Object是所有类的父类,包含在jdk里面,但并不适合绝大多数场景,通常需要重写

public boolean equals(Object obj) {        return (this == obj);    }

equals的作用:

用于判断两个变量是否是对同一个对象的引用,即堆中的内容是否相同,返回值为布尔类型

equals的基本使用:

boolean b = obj1.equals(obj2);

String类型比较不同对象内容是否相同,应该用equals,因为==用于比较引用类型和比较基本数据类型时具有不同的功能。

2.整理String类的Length()charAt()、 getChars()replace()、 toUpperCase()、 toLowerCase()trim()toCharArray()使用说明、阅读笔记发表到博客园。

Length():

 返回此字符串的长度。长度是等于Unicode代码单元中的字符串的数目

public int length() {

        return value.length;

}

charAt():

charAt(intindex)方法是一个能够用来检索特定索引下的字符的String实例的方法.charAt()方法返回指定索引位置的char值。索引范围为0~length()-1.:str.charAt(0)检索str中的第一个字符,str.charAt(str.length()-1)检索最后一个字符.

public char charAt(int index) {

        if ((index < 0) || (index >= value.length)) {

            throw new StringIndexOutOfBoundsException(index);

        }

        return value[index];

}

getChars()

getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)将字符从此字符串复制到目标字符数组。 
要复制的第一个字符在索引 srcBegin 处;要复制的最后一个字符在索引 srcEnd-1 处(因此要复制的字符总数是 srcEnd-srcBegin)。要复制到 dst 子数组的字符从索引 dstBegin 处开始,并结束于索引.例如
String str = "abcdefghikl";
Char[] ch = new char[8];
str.getChars(2,5,ch,0);
就是从str的第二个字母开始一直复制到第五个,一共是3个字符,ch的第一个开始接受

 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {

        if (srcBegin < 0) {

            throw new StringIndexOutOfBoundsException(srcBegin);

        }

        if (srcEnd > value.length) {

            throw new StringIndexOutOfBoundsException(srcEnd);

        }

        if (srcBegin > srcEnd) {

            throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);

        }

        System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);

}

replace()

public String replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 如果 oldChar 在此 String 对象表示的字符序列中没有出现,则返回对此 String 对象的引用。否则,创建一个新的 String 对象,它所表示的字符序列除了所有的 oldChar 都被替换为 newChar 之外,与此 String 对象表示的字符序列相同。 示例: "mesquite in your cellar".replace('e', 'o') returns "mosquito in your collar" "the war of baronets".replace('r', 'y') returns "the way of bayonets" "sparring with a purple porpoise".replace('p', 't') returns "starring with a turtle tortoise" "JonL".replace('q', 'x') returns "JonL" (no change) 参数: oldChar - 原字符。 newChar - 新字符。 返回: 一个从此字符串派生的字符串,它将此字符串中的所有 oldChar 替代为 newChar

public String replace(char oldChar, char newChar) {

        if (oldChar != newChar) {

            int len = value.length;

            int i = -1;

            char[] val = value; /* avoid getfield opcode */

            while (++i < len) {

                if (val[i] == oldChar) {

                    break;

                }

            }

            if (i < len) {

                char buf[] = new char[len];

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

                    buf[j] = val[j];

                }

                while (i < len) {

                    char c = val[i];

                    buf[i] = (c == oldChar) ? newChar : c;

                    i++;

                }

                return new String(buf, true);

            }

        }

        return this;

    }

toUpperCase()

将在此字符串中的所有字符为大写

public String toUpperCase() {

        return toUpperCase(Locale.getDefault());

    }

toLowerCase()

将在此字符串中的所有字符为小写

public String toLowerCase() {

        return toLowerCase(Locale.getDefault());

    }

trim()

trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除

  public String trim() {

        int len = value.length;

        int st = 0;

        char[] val = value;    /* avoid getfield opcode */

        while ((st < len) && (val[st] <= ' ')) {

            st++;

        }

        while ((st < len) && (val[len - 1] <= ' ')) {

            len--;

        }

        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;

    }

toCharArray()

意思是把别的数据转换成字符数组

 public char[] toCharArray() {

        // Cannot use Arrays.copyOf because of class initialization order issues

        char result[] = new char[value.length];

        System.arraycopy(value, 0, result, 0, value.length);

        return result;

    }

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

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

设计思想:输入一个字符串,然后选择加密或解密,加密。加密就是将字符后移3位,解密就是将字符串向前移3位,然后输出。

程序流程图:

源代码:

import java.util.*;

public class Jiami {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);//可以读出输入数

System.out.println("输入1或2(1加密,2解密):");

int a=sc.nextInt();

if(a==1)

{

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

String b=sc.next();

String g=new String();

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

{

char c=b.charAt(i);//将b的字符分离出来给c

if(c>='A'&&c<='W')

{

c+=3;

}

else

{

c-=23;

}

g=g+c;

}

System.out.println("加密的字符串为:"+g);

}

if(a==2)

{

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

String d=sc.next();

String h=new String();

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

{

char e=d.charAt(i);//将d的字符分离出来给e

if(e>='D'&&e<='W')

{

e-=3;

}

else

{

e+=23;

}

h=h+e;

}

System.out.println("解密的字符串为:"+h);

}

  }

}

结果截图

原文地址:https://www.cnblogs.com/sz20142898/p/4906023.html