简体   繁体   中英

Trying to output text of a list array in a random order but i keep getting an error with textView.setText(myList[rando]);

With the following the myList[rando] is underlined and it says "The type of expression must be an array type but it resolved to List"

textView.setText(myList[rando]);

Here is my code:

    public void OnButtonClickDisplayFact(View view)
    {

    List<String> myList = new ArrayList<String>();
    myList.add("111");
    myList.add("222");
    myList.add("333");

    int rando = (int) (Math.random() * 3);
    TextView textView = (TextView)findViewById(R.id.randomFactText);
    textView.setText(myList[rando]);


    }

To access elements of an array list, use the get method, that is

myList.get(rando)

myList[rando] only works for arrays

You're using array notation where get is required

textView.setText(myList.get(rando));

Have a read of the Collections Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM