Yeah, I'm trying to figure out how to print the elements of an array list in a non-static way. Here's the array list function definition class:
package samsExperiments; import java.util.ArrayList; public class SamsArrays { public static ArrayList<String> samsStaticArrayList() { ArrayList x = new ArrayList(); x.add("A"); x.add("B"); x.add("C"); x.add("D"); return(x); } public ArrayList<String> samsArrayList() { ArrayList x = new ArrayList(); x.add("A"); x.add("B"); x.add("C"); x.add("D"); return(x); } }
And here's the class with the main method:
package samsExperiments; import java.util.Scanner; import java.util.Arrays; import java.util.ArrayList; import java.util.Random; import static samsExperiments.StaticMethodExample.*; import java.lang.StringBuilder; import java.text.DecimalFormat; import java.util.Arrays; import java.util.InputMismatchException; import customExceptions.IntegerOutOfRangeException; public class SamsExperimentsMain { public static void main(String[] args){ SamsArrays.samsStaticArrayList();//call a static method SamsArrays sams = new SamsArrays(); ArrayList<String> x = sams.samsArrayList(); for(int i = 0; i < x.length(); i++) { System.out.println(x[i]); } } }
The error on line 26 says, "The method length() is undefined for the type ArrayList<String>".
The error on line 27 says, "The type of the expression must be an array type but it resolved to ArrayList<String>".
How do I fix this?