Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default ArrayList to Array program

    Hello,

    I am a student new to CIS and of course new to java. I have an assignment where I need to rewite a class called course w/o changing the original contract of the course class(definition of methods and constructors can't be changed). I need to use an ArrayList to replace an array to store students. I have worked some of the bugs out and have run it, but have come into an ArrayIndexOutOfBoundsException error. When I run the program I get the first line printed correctly followed by the error and a line of "null" statements numbering 13 which is the total number of times addStudent is used in the original program. I've toyed with it for a couple of days and still can't get it to work. The code is posted below, if anyone out there can help it would be appreciated (pardon any incorrectly used terminology, as I am very new to both programming and java). Error as follows:
    Java Code:
    run:
    Number of students in course1: 13
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
    	at Exercise11_05.main(Exercise11_05.java:30)
    null, null, null, null, null, null, null, null, null, null, null, null, null, Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)
    Program:
    Java Code:
    //Programming Exercise 11.5. Use the following template for the program: 
    import java.util.*;
    public class Exercise11_05 {
      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");
        course1.addStudent("Susan Kennedy");
        course1.addStudent("John Kennedy");
        course1.addStudent("Kim Johnson");
        course1.addStudent("S1");
        course1.addStudent("S2");
        course1.addStudent("S3");
        course1.addStudent("S4");
        course1.addStudent("S5");
        course1.addStudent("S6");
        course1.addStudent("S7");
          
        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("S1");
        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] + ", ");
        
        course1.clear();
        System.out.println("Number of students in course1: "
          + course1.getNumberOfStudents());
      }
    }
    
    class Course {
      private String courseName;
      private ArrayList students = new ArrayList();
      private int numberOfStudents;
        
      public Course(String courseName) {
        this.courseName = courseName;
      }
      
      public void addStudent(String student) {
        // Your code here
        students.add(student);
      }
       
      public String[] getStudents() {
        // Write your code here
        String [] s1 = new String [numberOfStudents];
        for (int i = 0; i < students.size(); i++) {
            //s1[i] = students[i];
            s1.toArray(students);
          }
          return s1;
      }
    
      public int getNumberOfStudents() {
        // Write your code here
          for (int i = 0; i< students.size(); i++) {
              numberOfStudents++;
          }
          return numberOfStudents;
      }  
    
      public String getCourseName() {
        return courseName;
      }  
      
      public void dropStudent(String student) {
        // Write your code here
          students.remove(student);
      }
      
      public void clear() {
        // Write your code here
          students.clear();
      }
    }
    Last edited by pbrockway2; 03-11-2012 at 10:23 PM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: ArrayList to Array program

    Your getNumberOfStudents() method looks strange/dangerous.

    What I mean by dangerous is that it actually alters the state of the Course instance. Each time you call it you increment numberOfStudents variable. The first time you call it, it will dutifully return 13, but the second time you call it you get the result 26. This is causing the ArrayIndexOutOfBoundsException when you print the students in the array once it hits the 14th student (who doesn't exist.)

    Java Code:
    System.out.println("Number of students in course1: "
          + course1.getNumberOfStudents());
    String[] students = course1.getStudents();
    for (int i = 0; i < course1.getNumberOfStudents(); i++) {
            // altered so we can see the index value i
        System.out.print("i=" + i + ":" + students[i] + ", ");
    }
    (Braces added, because braces are good.)

    What should you do about this?

    Think about a simpler way of returning the number of students: one based on the list methods that you can call on students. Also think about whether you need the numberOfStudents variable at all. There's no reason why getStudents() shouldn't use the public getNumberOfStudents() method, rather than directly accessing the variable.

    -----

    Welcome to the forum! (Sorry, should have said that to start with). I've added code tags to your post to make it more readable. Ask, if you can't figure out how they work.

Similar Threads

  1. Array vs ArrayList
    By Lund01 in forum New To Java
    Replies: 5
    Last Post: 10-14-2010, 10:41 AM
  2. Converting an array program to ArrayList
    By Adomini in forum New To Java
    Replies: 4
    Last Post: 10-13-2010, 07:22 PM
  3. 2D array of ArrayList (technically a 3D array)
    By Lil_Aziz1 in forum New To Java
    Replies: 3
    Last Post: 05-15-2010, 08:11 PM
  4. Array and arraylist
    By MIA6 in forum New To Java
    Replies: 3
    Last Post: 11-29-2009, 02:22 AM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 12:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •