如何在Java中使charAt方法与数组一起工作?

huangapple 未分类评论46阅读模式
英文:

how can I make charAt method work with arrays in java?

问题

可以有人帮我解决这个问题吗?我需要让 charAt 方法正常工作,但我不知道该怎么做。

class AsciiCharSequence implements java.lang.CharSequence/* 扩展/实现 */ {
    // 实现部分
    byte[] array;

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

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

    *@Override
    public char charAt(int i) {
        return (char) array.length(i);
    }*
}
英文:

can someone help me with this problem ? i have to make method charAt to work but i dont know how..

class AsciiCharSequence implements java.lang.CharSequence/* extends/implements */ {
    // implementation
    byte[] array;

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

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

    *@Override
    public char charAt(int i) {
        return (char) array.length(i);
    }*

答案1

得分: 0

尝试这样写:

    @Override
    public char charAt(int i) {
        return (char) array[i];
    }

由于您有一个数组并且想要返回特定数组中的字符。在您的问题中,您没有返回特定索引值。

英文:

Try this:

@Override
public char charAt(int i) {
    return (char) array[i];
}

Since you have an array and want to return the character at a particular array. In your question you are not returning a particular index value.

答案2

得分: 0

使用[]来访问数组中的元素。但不要忘记抛出所需的异常,就像在CharSequence文档中声明的那样(如果访问了数组中的不正确元素,则会抛出另一种类型的异常)。

public class AsciiCharSequence implements CharSequence {

    private static final char[] EMPTY_ARRAY = new char[0];

    private final char[] arr;

    public AsciiCharSequence(char[] arr) {
        this.arr = arr == null || arr.length == 0 ? EMPTY_ARRAY : Arrays.copyOf(arr, arr.length);
    }

    private AsciiCharSequence(char[] arr, int start, int end) {
        this.arr = arr == null || arr.length == 0 || start == end ? EMPTY_ARRAY : Arrays.copyOfRange(arr, start, end);
    }

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

    @Override
    public char charAt(int i) {
        if (i < 0 || i >= length())
            throw new IndexOutOfBoundsException();

        return arr[i];
    }

    @Override
    public AsciiCharSequence subSequence(int start, int end) {
        if (start < 0 || end < 0)
            throw new IndexOutOfBoundsException();
        if (end > length())
            throw new IndexOutOfBoundsException();
        if (start > end)
            throw new IndexOutOfBoundsException();

        return new AsciiCharSequence(arr, start, end);
    }

    @Override
    public String toString() {
        return IntStream.range(0, arr.length).mapToObj(i -> String.valueOf(arr[i])).collect(Collectors.joining());
    }
}
英文:

Use [] to access element in the array. But do not forget about throwing required exception, like it declared in CharSequence documentation (if you access not correct element in an array, another type of exception will be thorwn).

public class AsciiCharSequence implements CharSequence {

    private static final char[] EMPTY_ARRAY = new char[0];

    private final char[] arr;

    public AsciiCharSequence(char[] arr) {
        this.arr = arr == null || arr.length == 0 ? EMPTY_ARRAY : Arrays.copyOf(arr, arr.length);
    }

    private AsciiCharSequence(char[] arr, int start, int end) {
        this.arr = arr == null || arr.length == 0 || start == end ? EMPTY_ARRAY : Arrays.copyOfRange(arr, start, end);
    }

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

    @Override
    public char charAt(int i) {
        if (i &lt; 0 || i &gt;= length())
            throw new IndexOutOfBoundsException();

        return arr[i];
    }

    @Override
    public AsciiCharSequence subSequence(int start, int end) {
        if (start &lt; 0 || end &lt; 0)
            throw new IndexOutOfBoundsException();
        if (end &gt; length())
            throw new IndexOutOfBoundsException();
        if (start &gt; end)
            throw new IndexOutOfBoundsException();

        return new AsciiCharSequence(arr, start, end);
    }

    @Override
    public String toString() {
        return IntStream.range(0, arr.length).mapToObj(i -&gt; String.valueOf(arr[i])).collect(Collectors.joining());
    }
}

答案3

得分: 0

以下是您要翻译的内容:

为了更好地理解字符集,可以参考来自<b> AbstractStringBuilder</b> 的以下代码:

 @Override
public char charAt(int index) {
    checkIndex(index, count);
    if (isLatin1()) {
        return (char)(value[index] &amp; 0xff);
    }
    return StringUTF16.charAt(value, index);
}
英文:

For better understanding about character set can be refer below code from <b> AbstractStringBuilder</b>

 @Override
public char charAt(int index) {
    checkIndex(index, count);
    if (isLatin1()) {
        return (char)(value[index] &amp; 0xff);
    }
    return StringUTF16.charAt(value, index);
}

huangapple
  • 本文由 发表于 2020年5月4日 17:23:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/61588932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定