Android Studio - Array、ArrayList、List - JAVA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35118003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio - Array, ArrayList, List - JAVA
提问by osiic21
I am making a guessing game for as my School project, but I am quite new to Android Studio and Java.
我正在制作一个猜谜游戏作为我的学校项目,但我对 Android Studio 和 Java 还是很陌生。
At the moment my code looks like this:
目前我的代码如下所示:
public class MainActivity extends AppCompatActivity {
public int score = 0;
int random = random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button correct = (Button) findViewById(R.id.correct);
Button other = (Button) findViewById(R.id.other);
Button newGame = (Button) findViewById(R.id.newGame);
TextView words = (TextView) findViewById(R.id.words);
}
public Integer random (){
int random = (int )(Math.random() * 5);
return random;
}
private String list[] =
{"Dog", "Cat", "Mouse", "Elephant", "Rat", "Parrot"};
public void clickedButton(View view) {
TextView words = (TextView) findViewById(R.id.words);
if (view.getId()== R.id.newGame)
{
words.setText(list[random]);
score = 0;
Log.i("score", "score = " + score);
}
if (view.getId() == R.id.correct)
{
// set maybe new array so new textview does not give the same value
words.setText(list[random]);
score = score +1;
Log.i("score", "score = " + score);
}
if (view.getId() == R.id.other)
{
// set maybe new array so new textview does not give the same value
words.setText(list[random]);
Log.i("score", "score = " + score);
}
}
}
}
Idea is simple. I have my array with (at the moment) 6 words in it. So as I launch the game it gives me a random word on screen which I have to describe to others. If they guess it right I press correct, I get another word, if not I can pass and get another word... Well the problem I see is that there is chance that I get the same word over again. So I thought there should be a way how to fix this problem.
想法很简单。我的数组(目前)有 6 个字。因此,当我启动游戏时,它会在屏幕上给我一个随机词,我必须向其他人描述。如果他们猜对了,我按正确,我会得到另一个词,如果没有,我可以通过并得到另一个词......好吧,我看到的问题是我有可能再次得到相同的词。所以我认为应该有办法解决这个问题。
I thought about adding like if statements like if index 1 or 2 or 3 then give another word but if I had 100 words it would be monkey work to do it.
我想过添加 if 语句,例如 if index 1 或 2 或 3 然后再给出另一个词,但如果我有 100 个词,那么做这件事就太费劲了。
So I think there should be a chance how I can delete words from like temp. array, but there is no in-built method to do it.
所以我认为应该有机会如何从 temp 中删除单词。数组,但没有内置的方法来做到这一点。
I was reading that there are those arrayLists and lists but would not it be easier to use Array? and maybe some tips how to?
我读到有那些 arrayLists 和列表,但使用 Array 不是更容易吗?也许一些提示如何?
采纳答案by Pooya
You can first Create an ArrayList
(because it is more flexible):
您可以先创建一个ArrayList
(因为它更灵活):
List<String> list;
Then initialize the object in the constructor and shuffle the ArrayList
like below:
然后在构造函数中初始化对象并ArrayList
像下面这样混洗:
list = new ArrayList<String>();
list.add("Dog");
list.add("Cat");
...
//or alternatively list.addAll(Arrays.asList(normalArray)); if you have your data in array
Collections.shuffle(list,new Random(System.nanoTime()); //randomly shuffles your list
Then you can keep track of the index of the last item you read:
然后您可以跟踪您阅读的最后一项的索引:
int index = 0;
And every time you read an item just increase the index
:
每次您阅读一个项目时,只需增加index
:
words.setText(list.get(index++));
This will do the trick
这将解决问题
回答by Riley Carney
While there are a variety of ways solve your issue, personally I like mixing the array up (swapping places using random), then just keep an index
on what word you are on in the array, so you don't have to keep track of the words in it.
虽然有多种方法可以解决您的问题,但我个人喜欢混合数组(使用随机交换位置),然后只需index
记住您在数组中的单词,这样您就不必跟踪其中的话。
I wrote a code for a similar program for doing a random shuffle on a deck of cards below, it goes through the int times
amount, but you can remove it altogether and have a static loop value if you'd like:
我为一个类似的程序编写了一个代码,用于在下面的一副纸牌上进行随机洗牌,它通过int times
数量,但如果您愿意,您可以将其完全删除并具有静态循环值:
// ...
// Random Shuffle
// Tip: Use this method to shuffle, may not be the fastest (not noticeable however), but it makes your array totally randomized.
public static void totalRandom(String[] array, int times)
{
// Random Initialize
Random randomGenerator = new Random();
// Variables
int first = 0;
int second = 0;
// Swap
for (int i = 0; i < times; i++)
{
// Generates number from 0 to array length
first = randomGenerator.nextInt(array.length);
second = randomGenerator.nextInt(array.length);
String word = array[second];
array[second] = array[first];
array[first] = word;
}
}
// ...
then after in your class you would want to write something along the lines of...
然后在你的课后你会想写一些类似......
//...
int index = 0;
// ...
// Code for user input
// ...
if (userInput.equals(array[index]))
{
// do stuff
index++;
}
// ...
回答by skymedium
You can cache your last word. Add a String member variable.
您可以缓存您的最后一句话。添加一个 String 成员变量。
...
public int score = 0;
public String cache; // new
Instead of using
而不是使用
words.setText(list[random]);
you can call a method to request a new word. You also save this word to the cache:
你可以调用一个方法来请求一个新词。您还将这个词保存到缓存中:
...
cache = getRandomWord();
words.setText(cache);
...
Your new method looks like this:
您的新方法如下所示:
private String getRandomWord(){
int random = random();
String temp = list[random];
if(temp.equals(cache)){
temp = getRandomWord();
}
return temp;
}
Just put this method under your clickButton method. It takes a random word. Is the word equal to the previous word, the method will call itself again to pick another word. If not, it will return the new word.
只需将此方法放在您的 clickButton 方法下即可。它需要一个随机词。如果单词等于前一个单词,该方法将再次调用自身以选择另一个单词。如果不是,它将返回新单词。
There is no need to shuffle ArrayLists.
没有必要对 ArrayLists 进行洗牌。
回答by sakiM
I think a more simple way is pop every shown text out from a copied list.
我认为更简单的方法是从复制的列表中弹出每个显示的文本。
//when start a new game.
ArrayList<String> currentGameList = YourList.clone();
//when need next word.
String displayText = currentGameList.remove(randomIntInRestSize); //after called remove, you will get the object at list(index), and the object will be removed from the list.
Every game started, you just need to clone a new list from the original word list and play with the list.
每个游戏开始,您只需要从原始单词列表中克隆一个新列表并使用该列表进行游戏。