我已经对此进行了不少搜索,但只能找到不适用的实现(例如使用随机,我已经拥有)。很高兴的菜鸟问题,非常感谢任何帮助。
我有一个字符串数组,目前我通过一个简单的函数返回一个随机函数,我将其实现为按钮的onCLick值,代码如下。因此,每次有人点击bottun时,都会设置一个新字符串。
我想要做的是从上到下遍历数组,一次返回一个字符串,然后重新开始。我怀疑可能有几种方法可以做到这一点,任何建议?非常感谢提前!
public class FirstActivity extends Activity {
private TextView myStringView;
private static final Random RANDOM = new Random();
String myString;
//....my other code
private static final String[] MYSTRINGS = {
"string a",
"string b",
"string c"
};
public void generateString(View v) {
int myStringsLength = MYSTRINGS.length;
String myString = MYSTRINGS[RANDOM.nextInt(myStringsLength)];
myStringView.setText(myString);
}
}
答案 0 :(得分:5)
创建自己的功能:
private static int counter = 0;
private int nextInt(int length) {
return (counter++) % length;
}
然后简单地调用它而不是RANDOM.nextInt()
答案 1 :(得分:1)
这应该有效:
public class FirstActivity extends Activity {
private TextView myStringView;
private int stringIndex;
String myString;
//....my other code
private static final String[] MYSTRINGS = {
"string a",
"string b",
"string c"
};
public void generateString(View v) {
int myStringsLength = MYSTRINGS.length;
String myString = MYSTRINGS[stringIndex];
stringIndex = (stringIndex + 1) % myStringsLength;
myStringView.setText(myString);
}
}
答案 2 :(得分:1)
您基本上想要实现一个循环数组。你可以通过在课堂上保留一个计数器来做到这一点:
public void generateString(View v)
{
int index = count % MYSTRINGS.length;
count++;
String myString = MYSTRINGS[index];
myStringView.setText(myString);
}
答案 3 :(得分:0)
静态计数器可以解决问题。
public class FirstActivity extends Activity {
private static int counter = 0;
private TextView myStringView;
private static final Random RANDOM = new Random();
String myString;
//....my other code
private static final String[] MYSTRINGS = {
"string a",
"string b",
"string c"
};
public void generateString(View v) {
String myString = MYSTRINGS[counter++];
if( counter == MYSTRINGS.length ) {
counter = 0;
}
myStringView.setText(myString);
}
}
答案 4 :(得分:0)
确实有几种方法可以实现这一点,这是解决问题的最佳方法,因为据我所知,它是保持一个表示数组中当前String的计数器,并返回相应的String值,如:
String[] arr = new String[] {"a", "b", "c"};
int counter = 0;
public String getStringFromArr() {
if(counter < arr.length) {
return arr[counter++];
} else {
counter = 0;
return arr[counter++];
}
}
这是认为这是最简单的实现。你可以进一步抽象它把它放在一个带有数组字符串的类中,并公开一个返回要使用的String的nextString()方法。
另一个好处是使用List而不是String []。
您现在应该从您的方法调用getStringFromArr()。
答案 5 :(得分:0)
你可以写一个永远循环的Iterator
:
public class Test {
public static void main(String args[]) {
System.out.println("Test");
new Test().test();
}
public void test() {
String[] strings = {"One",
"Two",
"Three"};
for (Iterator<String> s = new ArrayIterator<>(strings); s.hasNext();) {
System.out.println(s.next());
}
int i = 0;
for (Iterator<String> s = new ArrayIterator<>(strings, true); i < 10 && s.hasNext(); i++) {
System.out.println(s.next());
}
}
static class ArrayIterator<T> implements Iterator<T> {
private final T[] array;
private boolean loop = false;
private int i = -1;
private T next = null;
public ArrayIterator(T[] array, boolean loop) {
this.array = array;
this.loop = loop;
}
public ArrayIterator(T[] array) {
this(array, false);
}
@Override
public boolean hasNext() {
if (next == null) {
// Step 1.
i += 1;
if (i == array.length) {
if (loop) {
i = 0;
}
}
next = (i < array.length ? array[i] : null);
}
return next != null;
}
@Override
public T next() {
T it = next;
next = null;
return it;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported.");
}
}
}
打印:
One
Two
Three
One
Two
Three
One
Two
Three
One
Two
Three
One