Hi all,

I've been trying to do this for a few hours now, I've seen several suggestions that
castin the toArray() method to a (String[]) should work but this results in runtime errors for me. Code is below, any help would be appreciated.

Error is with the getStudents() method, there may be other errors but that's the one I'm concentrating on at the moment.

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
at Course.getStudents(Exercise10_09.java:61)
at Exercise10_09.main(Exercise10_09.java:19)

I figured this shouldn't work as casting can only promote types? Correct me if I'm wrong.

import java.util.ArrayList;

public class Exercise10_09 
{
  public static void main(String[] args) 
  {
    Course course1 = new Course("Data Structures");
    Course course2 = new Course("Database Systems");

    course1.addStudent("Peter Jones");
    course1.addStudent("Brian Smith");
    course1.addStudent("Anne Kennedy");

    course2.addStudent("Peter Jones");
    course2.addStudent("Steve Smith");

    System.out.println("Number of students in course1: "
      + course1.getNumberOfStudents());
    String[] students = course1.getStudents();
    for (int i = 0; i < course1.getNumberOfStudents(); i++)
      System.out.print(students[i] + ", ");

    System.out.println();
    System.out.print("Number of students in course2: "
      + course2.getNumberOfStudents());
    
    course1.dropStudent("Brian Smith");
    System.out.println("Number of students in course1: "
      + course1.getNumberOfStudents());
    students = course1.getStudents();
    for (int i = 0; i < course1.getNumberOfStudents(); i++)
      System.out.print(students[i] + ", ");
  }
}

class Course 
{
  private String courseName;
  private String instructor;
  private ArrayList students = new ArrayList();
  
  public Course(String courseName)
  {
	  this.courseName = courseName;
  }
  
  public Course(String courseName, String instructor)
  {
	  this.courseName = courseName;
	  this.instructor = instructor;
  }
  
  public void addStudent(String student)
  {
	  students.add(student);
  }
  
  public String[] getStudents()
  {
	  students.trimToSize();
	  String[] studenta = (String[])students.toArray();
	  return studenta;
  }
  
  public int getNumberOfStudents()
  {
	  students.trimToSize();
	  return students.size();
  }
  
  public String getCourseName()
  {
	  return courseName;
  }
  
  public void dropStudent(String student)
  {
	  students.remove(student);
  }
  
  public void clear()
  {
	  students.clear();
  }
  
  
	  
}

students is an ArrayList of Student objects, so of course you can't cast them to Strings. the toArray method returns an array of Students in this case.
Why are you converting an ArrayList to an array anyway?

students is an ArrayList of Student objects, so of course you can't cast them to Strings. the toArray method returns an array of Students in this case.
Why are you converting an ArrayList to an array anyway?

The exercise runs the main class listed, I have to implement it and I thought it would be smarter to use an array list rather than copying and increasing size of array every time it goes over initial capacity.

As far as I was aware it was a list of objects? Declaring ArrayList students, as I understand it, just names the reference variable students.

There is a method in the ArrayList method - toArray(Object[] a) - which may be usable in this context but I can't work it out. Any suggestions or has anyone got a way to return a String from an object?

I don't understand your problem. If you have an ArrayList containing Student objects, what is it you what to do?

I want to return a String array of the names of the students, I was hoping to be able to return the names of the objects as an array of strings thus solving my problem. I need to implement the method public String[] getStudents() and I chose to try and answer the exercise using arraylists which is a bit advanced for my current course. Basically I was given the main method and had to write the Course class from scratch in order to implement it.

public String[] getStudents()

{

students.trimToSize();

String[] studenta = (String[])students.toArray();

return studenta;

}

eturn the names of the objects as an array of strings

You want to extract the Students' names from the arraylist into a String array?
Use the size of the array list to create the array and then loop over the arraylist getting the name from each object in the arraylist and store it in the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.