2 Replies - 8986 Views - Last Post: 18 November 2007 - 07:30 PM Rate Topic: -----

#1 m_milam  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-September 07

using an array list to replace an array

Post icon  Posted 18 November 2007 - 12:27 PM

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;
	}
}




How would i rewrite the course class using an array list to replace an array to store students?

Is This A Good Question/Topic? 2

Replies To: using an array list to replace an array

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: using an array list to replace an array

Posted 18 November 2007 - 12:59 PM

It is pretty simple to use an ArrayList. Simply import it, replace your array declaration, then modify your functions to use ArrayList methods like add() and size().

import java.util.ArrayList;

class Course {
	private String name;
	
	// Create an ArrayList of students
	private ArrayList students = new ArrayList();
	
	public Course(String name) {
		this.name = name;
	}
	
	// Add a student by using ArrayList's add method
	public void addStudent(String student) {
		students.add(student);
	}
	
	// Return the ArrayList of students
	public ArrayList getStudents() {
		return students;
	}
	
	// No need to keep track of count, ArrayList has a size method
	public int getNumberOfStudents() {
		return students.size();
	}
	
	public String getName() {
		return name;
	}
}



Things to note in the change is how I got rid of your student count. ArrayList has a size method which will tell you how many students are in the list. Also we return the ArrayList from the getStudents() method.

If you have a modern version of Java, you will see it throw up a Xlint warning upon compiling. This is because recent versions of Java support a generic Arraylist implementation. If you know you are only going to do strings, you could define your arraylist like...

// Create an ArrayList of students Strings
private ArrayList<String> students = new ArrayList<String>();



And be sure to return ArrayList<String> from your getStudents() method. But either way, new or old version of java supports creating an ArrayList without generics. So you can safely ignore the warning if you so wish.

Enjoy!

"At DIC we be ArrayList<Code_Ninjas> = new ArrayList<Code_Ninjas>!" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 m_milam  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 15-September 07

Re: using an array list to replace an array

Posted 18 November 2007 - 07:30 PM

I appreciate the quick response. This helps clear things up.

View PostMartyr2, on 18 Nov, 2007 - 12:59 PM, said:

It is pretty simple to use an ArrayList. Simply import it, replace your array declaration, then modify your functions to use ArrayList methods like add() and size().

import java.util.ArrayList;

class Course {
	private String name;
	
	// Create an ArrayList of students
	private ArrayList students = new ArrayList();
	
	public Course(String name) {
		this.name = name;
	}
	
	// Add a student by using ArrayList's add method
	public void addStudent(String student) {
		students.add(student);
	}
	
	// Return the ArrayList of students
	public ArrayList getStudents() {
		return students;
	}
	
	// No need to keep track of count, ArrayList has a size method
	public int getNumberOfStudents() {
		return students.size();
	}
	
	public String getName() {
		return name;
	}
}



Things to note in the change is how I got rid of your student count. ArrayList has a size method which will tell you how many students are in the list. Also we return the ArrayList from the getStudents() method.

If you have a modern version of Java, you will see it throw up a Xlint warning upon compiling. This is because recent versions of Java support a generic Arraylist implementation. If you know you are only going to do strings, you could define your arraylist like...

// Create an ArrayList of students Strings
private ArrayList<String> students = new ArrayList<String>();



And be sure to return ArrayList<String> from your getStudents() method. But either way, new or old version of java supports creating an ArrayList without generics. So you can safely ignore the warning if you so wish.

Enjoy!

"At DIC we be ArrayList<Code_Ninjas> = new ArrayList<Code_Ninjas>!" :snap:

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1