酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

编写一个程序,完成构造串、判断串是否为空、返回串的长度、求子串等操作。

以下是一个简单的示例程序,实现了构造串、判断串是否为空、返回串的长度和求子串的操作。 ```java public class MyString { private char[] chars; public MyString(String str) { chars = str.toCharArray(); } public boolean isEmpty() { return chars.length == 0; } public int length() { return chars.length; } public MyString substring(int start, int end) { if (start < 0 || end > chars.length) { throw new IndexOutOfBoundsException(); } char[] subChars = new char[end - start]; System.arraycopy(chars, start, subChars, 0, end - start); return new MyString(new String(subChars)); } public static void main(String[] args) { MyString str = new MyString("Hello World"); System.out.println("Length: " + str.length()); System.out.println("Is empty? " + str.isEmpty()); System.out.println("Substring: " + str.substring(6, 11)); } } ``` 该程序通过将字符串转换为字符数组进行操作,并提供了构造串、判断串是否为空、返回串的长度和求子串的方法。在主函数中示例了如何使用这些方法。 [2024-05-27 18:35:13 | AI写代码神器 | 778点数解答]

相关提问