0

There appears to be a number of similar questions been asked here but they do not seem to answer or solve my issue.

I have created an ArrayList and filled with random numbers,, these random numbers need to be displayed in Textviews in a LinearLayout.

                Random rand = new Random();
        List<Integer> list1 = new ArrayList<Integer>();
        List<Integer> list2 = new ArrayList<Integer>();
        for (int i=1; i<2; i++) {
            for (int mbs=1; mbs <= 4; mbs++) {
                int randNum1 = rand.nextInt(37 - 1) + 1;
                list1.add(randNum1);
                Collections.shuffle(list1);

                for (int j = 1; j<=2; j++){
                    int randNum2 = rand.nextInt(16 - 1)+1;
                    list2.add(randNum2);
                    Collections.shuffle(list2);
                }
            }
        }

    }

I used ( this is just one of the TextViews )

             TextView tv1l1 = findViewById(R.id.tv1l1);

Then wanted to put the random number from list1 or list2 into this using

    tv1l1.setText(list1(0));

I get error message saying can not resolve list1.

I would have thought that what I am trying to do would be easy but for some reason I can not get it, any help ?

2
  • Try list1.get(0) instead of list1(0): tv1l1.setText(list1.get(0)); Commented Nov 18, 2019 at 5:14
  • list1.get(0) will return its forst element, Its not array, Use methods for add and retrive elements Commented Nov 18, 2019 at 5:50

3 Answers 3

1

tv1l1.setText(list1(0)); need to be called in the same method where you define List<Integer> list1 = new ArrayList<Integer>();. Otherwise, you need to define list1 as a class level variable.

Sign up to request clarification or add additional context in comments.

Comments

0

The statement list1(0) inside
tv1l1.setText(list1(0)); is treated as a function call with function name list1 passing "0" as an argument. Since there exists no such function named list1(int), the error is correct.

What you should be using instead is tv1l1.setText(list1.get(0)); which says, get the first element (index = 0) from the list1 variable, declared above as List<Integer> list1

Comments

0

Check the definitions for the variable scopes,

Member Variables (Class Level Scope)

These variables must be declared inside class (outside any function). They can be directly accessed anywhere in class.

   public class Test
   {
    // All variables defined directly inside a class 
    // are member variables
    int a;
    private String b
    void method1() {....}
    int method2() {....}
    char c;
   }

Local Variables (Method Level Scope)

Variables declared inside a method have method level scope and can’t be accessed outside the method. Note : Local variables don’t exist after method’s execution is over.

  public class Test
  {
    void method1() 
    {
       // Local variable (Method level scope)
       int x;
    }
   }

Loop Variables (Block Scope) A variable declared inside pair of brackets “{” and “}” in a method has scope withing the brackets only.

public class Test 
{ 
    public static void main(String args[]) 
    { 
        { 
            // The variable x has scope within 
            // brackets 
            int x = 10; 
            System.out.println(x); 
        } 

        // Uncommenting below line would produce 
        // error since variable x is out of scope. 

        // System.out.println(x);  
    } 
} 

Some Important Points about Variable scope in Java:

  • In general, a set of curly brackets { } defines a scope.

  • In Java we can usually access a variable as long as it was defined within the same set of brackets as the code we are writing or within any curly brackets inside of the curly brackets where the variable was defined.

  • Any variable defined in a class outside of any method can be used by all member methods.

  • When a method has the same local variable as a member, this keyword can be used to reference the current class variable.

  • For a variable to be read after the termination of a loop, It must be declared before the body of the loop.

for more understanding go through the link https://www.geeksforgeeks.org/variable-scope-in-java/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.