用 Java 生成字母序列
我正在开始编程,我有一个生成字母序列的作业。在构造函数中,我们有一个输入,每次调用方法 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想法:用你的字母表准备一个字符串 A:“abc...ABC..Z”,并准备一个“增加”函数,给定 A 中的一个字符,返回以下字符。
初始化器将原始字符串设置在变量“s”中;然后 next() 获取“s”,检查最后一个字符,并对该字符调用“increase”函数。然后用结果替换字符串的最后一个字符。但是...如果新的字符是 A 的第一个,那么您还需要“增加”“s”中的下一个字符,依此类推。直到到达字符串的末尾:在这种情况下,只需添加第一个字符字母表的。
如果可以的话,您可以为此执行递归函数。否则,您可以使用初始化为字符串长度的索引“i”,并在需要获取字符串中的前一个字符时将其移回一个位置。
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.