习题8_Java常用类

1 .补全代码,完成对FirstLevel对象的深度克隆。

class FirstLevel implements Cloneable {
 int firstIntValue;
 double firstDoubleValue;
 SecondLevel second;

 public FirstLevel(int firstIntValue, double firstDoubleValue,SecondLevel second) {
   this.firstIntValue = firstIntValue;
   this.firstDoubleValue = firstDoubleValue;
   this.second = second;
 }
 @Override
 protected Object clone() throws CloneNotSupportedException {
   //利用Object的Clone方法,
   FirstLevel first = (FirstLevel) super.clone();
   //修改 first对象中引用类型成员变量,比如 second 成员变量引用,指向被复制的那个FirstLevel对象的second对象的拷贝,

   return first;
 }

 public void testProtected() throws CloneNotSupportedException {
   //跨包子类访问父类 ,访问FirstLevel自己从Object继承下来的Clone方法
   clone(); //alt + enter

   FirstLevel firstLevel = new FirstLevel(10, 20);
   firstLevel.clone();
 }
}

class SecondLevel implements Cloneable{
 int secondIntValue;
 double secondDoubleValue;
 ThirdLevel third;

 public SecondLevel(int secondIntValue, double secondDoubleValue,
     ThirdLevel third) {
   this.secondIntValue = secondIntValue;
   this.secondDoubleValue = secondDoubleValue;
   this.third = third;
 }

 @Override
 protected Object clone() throws CloneNotSupportedException {
   SecondLevel clone = (SecondLevel) super.clone();

   //修改SecondLevel这个对象中,引用类型的成员变量的值,让他指向,复制之后的成员变量所指向对象地址
   return clone;
 }
}

class ThirdLevel implements Cloneable{
 int thirdIntValue;
 double thirdDouleValue;

 public ThirdLevel(int thirdIntValue, double thirdDouleValue) {
   this.thirdIntValue = thirdIntValue;
   this.thirdDouleValue = thirdDouleValue;
 }
}

代码:

/*
    实现深度克隆的思路:
    1.首先每个类知道自己有哪些成员变量,成员变量中哪些是引用类型成员变量
    2.所以让每个类实现对自己的深度克隆方法
    3.实现深度克隆就变得简单了:
        a.首先利用super.clone()即Object的clone方法,完成对自己的克隆
        b.如果本类中有引用类型的成员,那么因为所有类都实现了对自己的深度克隆,
          所以直接调用引用类型成员所指向对象的clone方法,让他们自己完成自己的深度克隆
        c.将引用类型成员变量的值,让他们指向深度克隆后的对象
       这个思想有些类似于递归思想
 */
public class DeepClone {
    public static void main(String[] args) throws CloneNotSupportedException {
        ThirdLevel thirdLevel = new ThirdLevel(3,3.3);
        SecondLevel secondLevel = new SecondLevel(2, 2.2, thirdLevel);
        // 原对象
        FirstLevel firstLevel = new FirstLevel(1, 1.1, secondLevel);

        System.out.println("原对象(复制对象修改之前):\n" + firstLevel);
        // 利用Object的clone方法复制原对象
        FirstLevel cloneObj = (FirstLevel) firstLevel.clone();

        // 输出复制对象的内容
        System.out.println("复制对象(复制对象修改之前):\n" + cloneObj);

        //修改复制对象的值
        cloneObj.second.secondIntValue = 6;
        cloneObj.second.secondDoubleValue = 666.6;
        cloneObj.second.third.thirdIntValue = 4;
        cloneObj.second.third.thirdDouleValue = 4.4;

        System.out.println("原对象(复制对象修改之后):\n" + firstLevel);
        System.out.println("复制对象(复制对象修改之后):\n" + cloneObj);
    }

}

class FirstLevel implements Cloneable {
    int firstIntValue;
    double firstDoubleValue;
    SecondLevel second;

    public FirstLevel(int firstIntValue, double firstDoubleValue, SecondLevel second) {
        this.firstIntValue = firstIntValue;
        this.firstDoubleValue = firstDoubleValue;
        this.second = second;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        //利用Object的Clone方法,复制FirstLevel对象本身
        FirstLevel clone = (FirstLevel) super.clone();

        //再利用SecondLevel实现深度克隆clone()方法,完成对second所指向对象的深度克隆
        clone.second = (SecondLevel)second.clone();

        return clone;
    }

    @Override
    public String toString() {
        return "FirstLevel{" +
                "firstIntValue=" + firstIntValue +
                ", firstDoubleValue=" + firstDoubleValue +
                ",\nsecond=" + second +
                '}';
    }

}

class SecondLevel implements Cloneable{
    int secondIntValue;
    double secondDoubleValue;
    ThirdLevel third;

    public SecondLevel(int secondIntValue, double secondDoubleValue,
                       ThirdLevel third) {
        this.secondIntValue = secondIntValue;
        this.secondDoubleValue = secondDoubleValue;
        this.third = third;
    }
/*
    SecondLevel完成对自己的深度克隆
 */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        //利用Object的Clone方法,先克隆自己本身
        SecondLevel clone = (SecondLevel) super.clone();

        //再利用ThirdLevel实现深度克隆clone()方法,完成对third所指向对象的深度克隆
        clone.third = (ThirdLevel)third.clone();

        return clone;
    }

    @Override
    public String toString() {
        return "SecondLevel{" +
                "secondIntValue=" + secondIntValue +
                ", secondDoubleValue=" + secondDoubleValue +
                ",\nthird=" + third +
                '}';
    }
}

class ThirdLevel implements Cloneable{
    int thirdIntValue;
    double thirdDouleValue;

    public ThirdLevel(int thirdIntValue, double thirdDouleValue) {
        this.thirdIntValue = thirdIntValue;
        this.thirdDouleValue = thirdDouleValue;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "ThirdLevel{" +
                "thirdIntValue=" + thirdIntValue +
                ", thirdDouleValue=" + thirdDouleValue +
                '}';
    }
}

运行结果:

在这里插入图片描述

2.判断一个字符串是否是回文串。
例如"abc"不是对称字符串,“aba”、“abba”、“aaa”、"mnanm"是对称字符串。

代码:


public class Palindrome {
    public static void main(String[] args) {
        System.out.println("aabbaa" + 
        (isPalindrome("aabbaa") ? "是回文串" : "不是回文串"));

    }
    //判断目标字符串是否是字符串
    public static boolean isPalindrome(String s){
        //只需判断字符序列对称位置的字符是否相同

        //获取字符序列最后一个字符的下标
        int lastIndex = s.length() - 1;
        for(int i = 0;i < s.length() / 2;i++){
            if(s.charAt(i) != s.charAt(lastIndex - i)){
                //一旦有一组不相同,说明不是回文字符串
                return false;
            }
        }
        //循环结束,字符串对应位置的字符串都相同,说明是回文字符串
        return true;
    }
}

运行结果:

在这里插入图片描述

3.统计大串中小串出现的次数。举例:在字符串” woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次。

代码:


public class CountString {
    public static void main(String[] args) {
        String longStr = "javawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
        String aimStr = "java";//目标字符串
        int countStr = countString(longStr, aimStr);
        System.out.println(aimStr + "出现的次数:"+ countStr);
    }

    public static int countString(String longStr, String aimStr){
        int count = 0;//目标字符串出现的次数
        int positonValue = 0;//返回的字符串的位置值
        while((positonValue = longStr.indexOf(aimStr, positonValue)) != -1){
            positonValue++;
            count++;
        }
        return count;
    }
}

运行结果:

在这里插入图片描述

4.给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
注意:如果有多个最长回文串,返回其中一个即可
示例 1:
输入: “babad”
输出: “bab”
注意: “aba” 也是一个有效答案。
示例 2:
输入: “cbbd”
输出: “bb”

代码:


public class LongestPalindrome {
    public static void main(String[] args) {
        String s = "abbachhcjavaavajiopoijhnfeifojfs";
        System.out.println(s + "中的最长回文串是:" + longestPalindrome(s));
    }
    //该方法用来求得字符串s的最长回文子串
    public static String longestPalindrome(String s) {
        if (s == null) {
            return null;
        }

        if (s.length() == 1) {
            return s;
        }
        String longestPalindrome = "";

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

            //找到以第i个位置的字符为对称轴的最大回文子串(回文字符串字符个数为奇数)
            String tmp1 = findsubPalinrome(s, i, i);
            if (tmp1.length() > longestPalindrome.length()) {
                longestPalindrome = tmp1;
            }

            //找到以第 i 个字符和第 i + 1个字符中间位置为对称轴的最大回文子串(回文字符串字符个数为偶数)
            String tmp2 = findsubPalinrome(s, i, i + 1);
            if (tmp2.length() > longestPalindrome.length()) {
                longestPalindrome = tmp2;
            }
        }
        return longestPalindrome;
    }

    /*
        s为待判断的字符串
        在[0, low]找回文串的左半部分
        在[high, s.length - 1]找回文串的右半部分
        找到的最长的回文子串
     */
    private static String findsubPalinrome(String s, int low, int high) {
        char[] target = s.toCharArray();
        while (low >= 0 && high < s.length() && target[low] == target[high]) {
            low--;
            high++;
        }
        low++;
        high--;
        //此时low指向了回文串最左端的字符位置,high指向了回文串最右端的位置,所以该回文串包含的字符个数为 high - low + 1
        return new String(target, low, high - low + 1);
    }
}

运行结果:

在这里插入图片描述
#个人学习记录,如发现有错误之处,欢迎与我交流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值