Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 414,938 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,680 people online right now.Registration is fast and FREE... Join Now!



using an array list to replace an array Rate Topic: -----

#1 m_milam  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 15-September 07


Dream Kudos: 0

Share |

using an array list to replace an array

Post icon  Posted 18 November 2007 - 11:27 AM

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?
Was This Post Helpful? 1


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1434
  • View blog
  • Posts: 8,313
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: using an array list to replace an array

Posted 18 November 2007 - 11:59 AM

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
  • +
  • -

#3 m_milam  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 15-September 07


Dream Kudos: 0

Re: using an array list to replace an array

Posted 18 November 2007 - 06: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
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users