How do I pass an ArrayList from one method to another in Java?

  Kiến thức lập trình

I need to create an empty list in one method and save the reference. Then, I need to pass the reference to another method where the list is populated with shapes and their parameters.

public class Test {

public static void start() {
    ArrayList<Object> shapes = new ArrayList<>();
}

public static void create() {
    BufferedReader file = new BufferedReader(new FileReader("dataFile.txt"));
    
    if (file.readLine() == "Rectangle") {
        String shapeType = file.readLine();
        String name = file.readLine();
        Double length = Double.valueOf(file.readLine());
        Double width = Double.valueOf(file.readLine());
        Rectangle newShape = new Shape.Rectangle(name, length, width);
        shapes.add(newShape); // this is where this class cannot recognize "shapes" list

New contributor

OhmsLaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT