I'm trying to print contents of an array stored inside an ArrayList.
My constructor parameters are (String,String,String,String[], String, String).
When creating add. function to the arrayList the contents are stored in the String[] parameter.
But when using a getter method to return the String[], it launches an error: "Type mismatch: cannot convert from String to String[]".
Eclipse solution is to change getter method to String, but then the add. function doesn't work because the parameter should be String[].
Also all the .toString , .clone , etc, returns memory location not array contents. Desperate for help:! Here is part of my code:
public class NewRegandLogin {
private String alumniFirstName;
private String alumniLastName;
private String alumniId;
private Scanner scanner = new Scanner(System.in);
private String linkedInPage;
static ArrayList<NewRegandLogin> loginInformation = new ArrayList<>();
private String alumniIdImput;
private String passwordImput;
private String permanentPasword;
private String[] coursesList;
public NewRegandLogin(String alumniFirstName, String alumniLastName,String alumniId, String[] coursesList, String linkedInPage, String permanentPasword) {
this.alumniFirstName=alumniFirstName;
this.alumniLastName = alumniLastName;
this.alumniId = alumniId;
this.coursesList = coursesList;
this.linkedInPage = linkedInPage;
this.permanentPasword = permanentPasword;
}
public void setAlumniCourses() {
coursesList = new String[10];
for (int i=0; i < coursesList.length; i++) {
if(coursesList[i]==null) {
System.out.println("Completed Course Name: ");
coursesList[i]=scanner.next();
}
if(coursesList[i].equals("s") || coursesList[i].equals("S")) {
break;
}
}
}
public String[] getCourses() {
return Arrays.toString(coursesList);
}
main
public class Main {
static NewRegandLogin newRegAndLogin = new NewRegandLogin(null, null, null, null, null, null);
public static void main(String[] args) {
System.out.println("Please make a list of completed Courses: (Enter S to stop adding courses) ");
newRegAndLogin.setAlumniCourses();
loginInformation.add(newRegAndLogin);
printAlumniProfile();
}
public static void printAlumniProfile() {
for (int i = 0; i<NewRegandLogin.loginInformation.size();i++) {
System.out.println(((i+1)+"-"+ NewRegandLogin.loginInformation.get(i)));
System.out.println();
}
}
}
Output: 1-Alumni Name: Geri Glazer
Alumni ID: geri.glazer.she-codes
Courses completed: [Ljava.lang.String;@3dd3bcd
public String[] getCourses() {
return Arrays.toString(coursesList);
}
Arrays.toString
returns a String
, not a String[]
. If you want to return a String
, change the return type to String
.
public String getCourses() {
return Arrays.toString(coursesList);
}
If you want to return a single-element array, containing the string representation of coursesList
, you can wrap the String
in an array:
public String[] getCourses() {
return new String[]{Arrays.toString(coursesList)};
}
If you want to return the array, just return the array:
public String[] getCourses() {
return coursesList;
}
Or, more safely, return a defensive copy of the array (to prevent the caller changing the internal array):
public String[] getCourses() {
return Arrays.copyOf(coursesList, coursesList.length);
}
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.