Error: Cannot Find Symbol on Java; how to declare arrays

3825 views java
1

I am new to Java and do not know how to set an array. My reset method cannot access myList when I later call it in the main method...

public Hand(){
  ArrayList<Object> myList=new ArrayList<>();
 }

 public void reset(){
  myList.clear();
}

answered question

1 Answer

11

In your constructor you are simply creating an ArrayList object that passes out of scope. You need to declare it as a class variable. Try something like:

 ArrayList<Object> myList;
 public Hand(){
    myList=new ArrayList<>();
 }

 public void reset(){
    myList.clear();
 }

Read this for more information.

posted this

Have an answer?

JD

Please login first before posting an answer.