我想从一个外部类中调用theArrayList()方法,该类显示名为theList的对象arraylist内部的内容。我将如何做到这一点?

mendietaRAL.java

import java.util.ArrayList;

public class mendietaRAL {
  public static void theArrayList() {
    ArrayList<Object> theList = new ArrayList<Object>();

    theList.add(123);
    theList.add("Java");
    theList.add(3.75);
    theList.add("Summer C");
    theList.add(2018);

    for (int i = 0; i < theList.size(); i++) {
      System.out.print(theList.get(i));
    }

    theList.remove(1);
    theList.remove(4);

    for (int i = 0; i < theList.size(); i++) {
      System.out.print(theList.get(i));
    }
  }
}


mendietaRpgm2.java

public class mendietaRpgm2 {
  public static void main(String args[]) {

  }
}

最佳答案

public class mendietaRpgm2 {
  public static void main(String args[]) {
   mendietaRAL.theArrayList();
  }
}


请将您的班级名称更改为MendietaRpgm2。查看命名约定here
另外,删除未使用的ArrayList导入。

10-07 12:42