11 Replies - 11296 Views - Last Post: 25 July 2011 - 01:20 PM Rate Topic: -----

#1 Aquanun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-July 11

ArrayList constructor

Posted 25 July 2011 - 10:15 AM

Hey guys I need a few methods for adding and removing Student objects from a previous class but what I'm looking for here is the constructors for an ArrayList method, how do I make a constructor with and without parameters for an ArrayList? Is the no parameter constructor like this?:
ArrayList<Student> students = new ArrayList<Student>();


Thanks for any and all replies!
Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList constructor

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7949
  • View blog
  • Posts: 13,538
  • Joined: 19-March 11

Re: ArrayList constructor

Posted 25 July 2011 - 10:20 AM

That constructor will create an empty ArrayList which accepts anything that can be a Student.
If you have a pre-existing Collection of Students, pass that Collection as a parameter to the constructor to make an ArrayList with that Collection's contents.

Hope that helps.
Was This Post Helpful? 3
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: ArrayList constructor

Posted 25 July 2011 - 10:41 AM

The ArrayList class(it is a class and not a method) has three constructors(one you written and another that jon.kiparsky pointed). What you are doing there in your code is to create a collection to hold Student objects. The ArrayList has built in methods for adding and removing elements(if what you need is to add and remove Student objects). Unless, I think you will have to customize your Student class constructors....
Was This Post Helpful? 2
  • +
  • -

#4 Aquanun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-July 11

Re: ArrayList constructor

Posted 25 July 2011 - 10:50 AM

View Postsmohd, on 25 July 2011 - 10:41 AM, said:

The ArrayList class(it is a class and not a method) has three constructors(one you written and another that jon.kiparsky pointed). What you are doing there in your code is to create a collection to hold Student objects. The ArrayList has built in methods for adding and removing elements(if what you need is to add and remove Student objects). Unless, I think you will have to customize your Student class constructors....

Great thanks! And yeah I'm trying to basically create a database of Student objects using the ArrayList class, those methods to add and remove are just like students.add() and students.remove()?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: ArrayList constructor

Posted 25 July 2011 - 10:52 AM

The documentation is a great place to start!
Was This Post Helpful? 1
  • +
  • -

#6 Aquanun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-July 11

Re: ArrayList constructor

Posted 25 July 2011 - 11:18 AM

View Postmacosxnerd101, on 25 July 2011 - 10:52 AM, said:

The documentation is a great place to start!

Yeah I read the doc! Every example I seem to see has an array listed above the ArrayList and the add method is given like this:
Integer[] integers = {1, 2, 3, 4, 5};
ArrayList<Integer> integerList = new ArrayList<Integer>();

for (Integer element : integers)
    integerList.add(element);


Is there a way to have an ArrayList and not have to have an array with it? I'm trying to make a GUI database that can add, remove, update a student object from the ArrayList when the "Add" "Remove" or "Update" button is pressed so there can't be a static number of students, I just want the ArrayList to be able to add and remove students!
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: ArrayList constructor

Posted 25 July 2011 - 11:20 AM

Yes. You can use an ArrayList without an array. In fact, as the documentation states, the add() method only requires an element of matching type to what is being stored in the ArrayList. So if you are storing an ArrayList<String>, you can only pass a String to the add() method.
Was This Post Helpful? 1
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7949
  • View blog
  • Posts: 13,538
  • Joined: 19-March 11

Re: ArrayList constructor

Posted 25 July 2011 - 11:51 AM

View PostAquanun, on 25 July 2011 - 01:18 PM, said:

Is there a way to have an ArrayList and not have to have an array with it? I'm trying to make a GUI database that can add, remove, update a student object from the ArrayList when the "Add" "Remove" or "Update" button is pressed so there can't be a static number of students, I just want the ArrayList to be able to add and remove students!



You don't need an array, as mac has said. Those are examples of ways to populate an ArrayList.

Create your ArrayList<Student>, then create Students and use the add method to add them.

I'm not convinced that an ArrayList is the best choice for this application, but you should be able to make a toy application to exercise the ideas.
Was This Post Helpful? 1
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: ArrayList constructor

Posted 25 July 2011 - 12:04 PM

For more examples on how to use arrayLists look here and here
Was This Post Helpful? 2
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: ArrayList constructor

Posted 25 July 2011 - 12:05 PM

Locke has a tutorial on static arrays vs. ArrayLists, which you may find helpful as well.
Was This Post Helpful? 2
  • +
  • -

#11 Aquanun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-July 11

Re: ArrayList constructor

Posted 25 July 2011 - 01:19 PM

Thanks guys I'll definitely take a look at those tutorials! Thanks for all the replies, +1s all around!
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: ArrayList constructor

Posted 25 July 2011 - 01:20 PM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1