I have an ArrayList that contains the below elements of type class: ListClass
public class ListClass {
String requestPath; //parse
List<String> paramsMandatory; //name, query
List<String> paramsOptional; //company
boolean needBody; //true
String mimeType; //String, json
public String getRequestPath() {
return requestPath;
}
public void setRequestPath(String requestPath) {
this.requestPath = requestPath;
}
public List<String> getParamsMandatory() {
return paramsMandatory;
}
public void setParamsMandatory(List<String> paramsMandatory) {
this.paramsMandatory = paramsMandatory;
}
public List<String> getParamsOptional() {
return paramsOptional;
}
public void setParamsOptional(List<String> paramsOptional) {
this.paramsOptional = paramsOptional;
}
public boolean isNeedBody() {
return needBody;
}
public void setNeedBody(boolean needBody) {
this.needBody = needBody;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
}
Setting the properties of it in another class: PropSetter
public class PropSetter {
List<String> mp = new ArrayList<String>();
List<String> op = new ArrayList<String>();
public void setParameters() {
mp.add("force");
mp.add("name");
op.add("company");
op.add("location");
ListClass lc = new ListClass();
lc.setRequestPath("/parse");
lc.setParamsMandatory(mp);
lc.setParamsOptional(op);
lc.setNeedBody(false);
lc.setMimeType("String");
System.out.println("Set the props for ListClass");
}
}
I am trying to return an ArrayList that is of Type: ListClass
in the below way:
List<ListClass> cl = new ArrayList<ListClass>();
public void setCL() {
PropSetter ps = new PropSetter();
ps.setParameters();
ListClass lcl = new ListClass();
cl.add(lcl);
}
public List<ListClass> getPropList() {
return cl;
}
The method getPropList
returns the List of type: ListClass
. How do I access elements in it ? If it is a particular datatype, I could've used an Iterator or a foreach loop. But this is of a class type: ListClass
which I dont understand how to access the elements, particularly the lists: paramsMandatory & paramsOptional inside.
I tried to display the elements like:
CreateList cl = new CreateList();
cl.setCL();
List<ListClass> ll = cl.getPropList();
System.out.println("Size of Arraylist<ListClass>:" + ll.size());
for (ListClass l: ll) {
System.out.println("DS: " + l);
}
In my main class, I tried to see if I can print a parameter inside as below.
for (ListClass l: ll) {
System.out.println("DS: " + l.getRequestPath);
}
which gives me a compilation error: getRequestPath cannot be resolved or is not a field
I tried to print the size of array and it shows the right value:
System.out.println("Size of Arraylist<ListClass>:" + ll.size());
Size of Arraylist<ListClass>: 1
Could anyone let me know how can I access the elements coming from : getPropList
Look at the compiler error you are getting, namely:
getRequestPath cannot be resolved or is not a field
In other words, the compiler thinks that getRequestPath
is a member of class ListClass
- because you forgot to add parentheses so as to indicate to the compiler that getRequestPath
is a method. Hence you need to change that line of your code as follows:
System.out.println("DS: " + l.getRequestPath());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.