知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

  • 练习题

    • 选择题 & 填空题

    • 代码题

      • Boxes
      • The longest ascending sequence
      • Right Rotation
      • Book
      • Compact strings with AsciiCharSequence
        • Problem
        • Hint & Explain
        • Solution
          • Other solution 1
          • Other solution 2
      • Cinema
      • Calculate square
      • Count words
      • Robot control
      • Concat all strings without digits
      • Find the nearest number
      • Leaderboard
      • Split a list into sublists
      • Arithmetic average
      • Processing strings
  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • 练习题
  • 代码题
Jim
2022-06-28
目录

Compact strings with AsciiCharSequence

Interface

# Problem

Compact strings with AsciiCharSequence

https://hyperskill.org/learn/step/3082

注意

Wow! This problem is kind of tricky. If you're ready to put your thinking cap on, brace yourself and good luck! Otherwise, you can skip it for now and return any time later

Strings in Java implement java.lang.CharSequence interface. Since Java internally uses UTF-16, 2 bytes are required to store each char. At the same time, ASCII encoding allows storing character codes in one byte and includes all Latin letters, digits, and standard special characters. Compared to the standard String class, ASCII-character sequences require half the memory.

Write a class named AsciiCharSequence for storing ASCII-character sequences, that should:

  • implement the interface java.lang.CharSequence;
  • have a constructor that takes a byte array;
  • have methods int length(), char charAt(int idx), CharSequence subSequence(int from, int to), and String toString().

You can find the declaration of methods and their behavior in the description of java.lang.CharSequence (JavaDoc (opens new window) or sources).

Carefully check signatures of abstract methods of java.lang.CharSequence interface, especially subSequence method. It accepts 2 integers: start index (inclusive) and end index (exclusive). The method returns an object of a class that implements java.lang.CharSequence interface. In this example it will be an instance of AsciiCharSequence class.

Note: the testing system will always pass correct input parameters to overridden methods.

P.S. This feature is supported since Java 9 in standard strings. For details, see this article on compact strings in Java 9 (opens new window).

# Hint & Explain

// 输入提示

# Solution

# Other solution 1

import java.util.*;

class AsciiCharSequence implements CharSequence {
    
    private final byte[] bytes;
    
    public AsciiCharSequence(byte[] bytes) {
        this.bytes = bytes.clone();
    }
    
    @Override
    public int length() {
        return this.bytes.length;
    }
    
    @Override
    public char charAt(int idx) {
        return (char) this.bytes[idx];
    }
    
    @Override
    public CharSequence subSequence(int from, int to) {
        byte[] sequence = new byte[to - from];
        
        for (int i = from; i < to; i++) {
            sequence[i - from] = this.bytes[i];
        }
        
        return new AsciiCharSequence(sequence);
    }
    
    @Override
    public String toString() {
        return new String(this.bytes, StandardCharsets.UTF_8);
    }
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# Other solution 2

import java.util.*;

class AsciiCharSequence implements CharSequence {
    private final byte[] charSequence;

    public AsciiCharSequence(byte[] charSequence) {
        this.charSequence = charSequence.clone();
    }

    @Override
    public int length() {
        return charSequence.length;
    }

    @Override
    public char charAt(int index) {
        return (char) charSequence[index];
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return new AsciiCharSequence(Arrays.copyOfRange(charSequence, start, end));
    }

    @Override
    public String toString() {
        return new String(charSequence);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
编辑 (opens new window)
#Java#Problem#OOP#Abstraction
上次更新: 2022/10/25, 17:57:56
Book
Cinema

← Book Cinema→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式