Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - Array, ArrayList, List - JAVA

Tags:

java

android

I am making a guessing game for as my School project, but I am quite new to Android Studio and 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.

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.

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.

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?

like image 467
osiic21 Avatar asked Jan 31 '16 19:01

osiic21


People also ask

Can I make an array of Arraylists in Java?

ArrayList of arrays can be created just like any other objects using ArrayList constructor.

What is difference between ArrayList and list in Android?

The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.

Which is better array or ArrayList in Java?

An array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one.

How do you add an array to an ArrayList in Java?

Using ArrayList. add() method to manually add the array elements in the ArrayList: This method involves creating a new ArrayList and adding all of the elements of the given array to the newly created ArrayList using add() method.


1 Answers

You can first Create an ArrayList (because it is more flexible):

List<String> list;

Then initialize the object in the constructor and shuffle the ArrayList like below:

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:

words.setText(list.get(index++));

This will do the trick

like image 140
Pooya Avatar answered Sep 28 '22 18:09

Pooya