Results 1 to 2 of 2
  1. #1
    toad is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default using an Array list

    Java Code:
    public class Course {
        private String name;
        private String[] students = new String[100];
        private int numberOfStudents;
        
        public Course(String name) {
            this.name = name;
        }
        
        public void addStudent(String student) {
            students[numberOfStudents] = student;
            numberOfStudents++;
        }
        
        public String[] getStudents() {
            return students;
        }
        
        public int getNumberOfStudents() {
            return numberOfStudents;
        }
        
        public String getName() {
            return name;
        }
    }
    I need to rewrite the course class using an ArrayList to replace an array to store students. I'm a day into learning about ArrayLists and I am terribly stuck.
    Last edited by JavaBean; 11-18-2007 at 08:40 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Trail: Collections
    List Implementations
    Java Code:
    import java.util.*;
    
    public class Course {
        private String name;
        // An ArrayList grows as you add to it.
        // Choose one of the following according to the
        // version of java you are using:
        // This is for j2se 1.5+ with generics:
        private List<String> students = new ArrayList<String>();
        // Use this for non-generics: j2se 1.4 or earlier:
        //private List students = new ArrayList();
        
        public Course(String name) {
            this.name = name;
        }
        
        public void addStudent(String student) {
            students.add(student);
        }
        
        public String[] getStudents() {
            int size = getNumberOfStudents();
            // Find this method in the ArrayList class api
            // Methods Summary section.
            return students.toArray(new String[size]);
        }
        
        public int getNumberOfStudents() {
            return students.size();
        }
        
        public String getName() {
            return name;
        }
    }

Similar Threads

  1. How to convert List to Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:37 PM
  2. Converting array to list and sorting it
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:36 PM
  3. Array List Problem
    By khamuruddeen in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 08:10 AM
  4. how to remove an object from the array list
    By cecily in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 02:26 PM

Posting Permissions

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