用 Java 生成字母序列

发布于 2025-01-11 10:51:56 字数 1106 浏览 0 评论 0原文

我正在开始编程,我有一个生成字母序列的作业。在构造函数中,我们有一个输入,每次调用方法 next() 时都会返回序列中的下一个值。

"","a","b","c",....."z","A","B",......"Z","aa","bb",.....,"zz",.....
if input is:
If the current string is:"z",so after calling next(),we get "A";
If the current string is:"Z",so after calling next(),we get "aa";
If the current string is:"az",so after calling next(),we get "ba";
If the current string is:"ZZZZZZ",so after calling next(),we get "aaaaaaa";

代码分配网在这里:

import  java.util.Iterator;

public class StringIterator implements Iterator<String> {

    //Complete the following methods
    public StringIterator(String start){
        // task 0
        throw new UnsupportedOperationException("Delete this line and implement this function");
    }

    public boolean hasNext(){
        // task 0
        //assume string length()<Integer. MAX_VALUE
       return true;
    }
    public String next(){
        // task 0
        throw new UnsupportedOperationException("Delete this line and implement this function");
    }
}

I am beginning at programing and I have an assignment to generate Alphabet sequence. In constructor we have an input, every time when call the method next() will return the next value in the sequence.

"","a","b","c",....."z","A","B",......"Z","aa","bb",.....,"zz",.....
if input is:
If the current string is:"z",so after calling next(),we get "A";
If the current string is:"Z",so after calling next(),we get "aa";
If the current string is:"az",so after calling next(),we get "ba";
If the current string is:"ZZZZZZ",so after calling next(),we get "aaaaaaa";

code assignmnet is here:

import  java.util.Iterator;

public class StringIterator implements Iterator<String> {

    //Complete the following methods
    public StringIterator(String start){
        // task 0
        throw new UnsupportedOperationException("Delete this line and implement this function");
    }

    public boolean hasNext(){
        // task 0
        //assume string length()<Integer. MAX_VALUE
       return true;
    }
    public String next(){
        // task 0
        throw new UnsupportedOperationException("Delete this line and implement this function");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2025-01-18 10:51:56

想法:用你的字母表准备一个字符串 A:“abc...ABC..Z”,并准备一个“增加”函数,给定 A 中的一个字符,返回以下字符。

初始化器将原始字符串设置在变量“s”中;然后 next() 获取“s”,检查最后一个字符,并对该字符调用“increase”函数。然后用结果替换字符串的最后一个字符。但是...如果新的字符是 A 的第一个,那么您还需要“增加”“s”中的下一个字符,依此类推。直到到达字符串的末尾:在这种情况下,只需添加第一个字符字母表的。

如果可以的话,您可以为此执行递归函数。否则,您可以使用初始化为字符串长度的索引“i”,并在需要获取字符串中的前一个字符时将其移回一个位置。

/**
 *
 */
package it.mixedstuff;

import java.util.Iterator;
import java.util.NoSuchElementException;;

/**
 * @author samuele m.
 *
 */
public class StringIterator implements Iterator<String>
{
    private static final String A = "abcABC";
    private String s;

    // Complete the following methods
    public StringIterator(String start)
    {
        s = start;
    }

    private char increase(char c)
    {
        int k = A.indexOf(c);
        if (k == -1) {
            throw new RuntimeException("Unsupported char");
        }
        k = (k + 1) % A.length();
        return A.charAt(k);
    }

    private char firstChar()
    {
        return A.charAt(0);
    }

    private boolean isFirst(char ch)
    {
        return (ch == firstChar());
    }

    public boolean hasNext()
    {
        return A.length() < Integer.MAX_VALUE;
    }

    public String next()
    {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        int i = s.length();
        while (--i >= 0) {
            // get char at position A[i]
            char c = s.charAt(i);
            // return the following one
            c = increase(c);
            // replace the char in the string
            s = s.substring(0, i) + c + s.substring(i + 1);
            // have we ended?
            if (!isFirst(c)) {
                return s;
            }
        }
        // if string is ended, we need to add the first char
        if (i < 0) {
            s = firstChar() + s;
        }
        return s;
    }

    public static void main(String[] args)
    {
        String base = "Bc";
        StringIterator s = new StringIterator(base);
        System.out.println("We start with " + base);
        for (int i = 0; i < 20; i++) {
            String n = s.next();
            System.out.println("Then there is " + n);
        }
    }

}

The idea: prepare a String A with your alphabet: "abc...ABC..Z", and prepare a "increase" function that, given a char inside A, returns the following one.

The initialiser sets the original string in a variable "s"; then the next() gets "s", checks the last character, and invokes the "increase" function on that char. Then replace the last character of the string with the result. But... if the new char is the first of A, then you need to "increase" also the next char in "s", etc. Until you reach the end of the string: in this case, simply add the first char of the alphabet.

You can do a recursive function for that if you are able to do it. Otherwise, you can use an index "i" initialised to the string length and move it back of one position whenever you need to get the previous char in the string.

/**
 *
 */
package it.mixedstuff;

import java.util.Iterator;
import java.util.NoSuchElementException;;

/**
 * @author samuele m.
 *
 */
public class StringIterator implements Iterator<String>
{
    private static final String A = "abcABC";
    private String s;

    // Complete the following methods
    public StringIterator(String start)
    {
        s = start;
    }

    private char increase(char c)
    {
        int k = A.indexOf(c);
        if (k == -1) {
            throw new RuntimeException("Unsupported char");
        }
        k = (k + 1) % A.length();
        return A.charAt(k);
    }

    private char firstChar()
    {
        return A.charAt(0);
    }

    private boolean isFirst(char ch)
    {
        return (ch == firstChar());
    }

    public boolean hasNext()
    {
        return A.length() < Integer.MAX_VALUE;
    }

    public String next()
    {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        int i = s.length();
        while (--i >= 0) {
            // get char at position A[i]
            char c = s.charAt(i);
            // return the following one
            c = increase(c);
            // replace the char in the string
            s = s.substring(0, i) + c + s.substring(i + 1);
            // have we ended?
            if (!isFirst(c)) {
                return s;
            }
        }
        // if string is ended, we need to add the first char
        if (i < 0) {
            s = firstChar() + s;
        }
        return s;
    }

    public static void main(String[] args)
    {
        String base = "Bc";
        StringIterator s = new StringIterator(base);
        System.out.println("We start with " + base);
        for (int i = 0; i < 20; i++) {
            String n = s.next();
            System.out.println("Then there is " + n);
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文