| Author |
I need to replace an array with an arraylist
|
Austin Trower
Greenhorn
Joined: Nov 06, 2012
Posts: 19
|
|
SUP guys I need to replace the array students With an ArrayList and for some reason I am getting an Error saying Course is already defined little help please
I searched and came up with using an array in an ArrayList but not how to replace it
|
 |
Jan Cumps
Bartender
Joined: Dec 20, 2006
Posts: 2350
|
|
Austin, Welcome to the Ranch!
Jan
|
OCUP UML fundamental
ITIL foundation
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
It would help if you'd TellTheDetails(←click), such as copy/pasting the exact, complete error message and indicating clearly which line is causing it.
Based on what you've said, the best guess that I have is that you've got another Course class defined in another .java file in your source path and/or its corresponding .class file on your classpath.
|
 |
Austin Trower
Greenhorn
Joined: Nov 06, 2012
Posts: 19
|
|
|
Okay i fixed the Course{ class problem now, can anybody give my advice about changeing the array students[] round abouts line 18 into an ArrayList
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
Austin Trower wrote:Okay i fixed the Course{ class problem now, can anybody give my advice about changeing the array students[] round abouts line 18 into an ArrayList
Start by googling for java collections tutorial or just go here: http://docs.oracle.com/javase/tutorial/collections/index.html
Once you've worked through that and become familiar with Lists, you should be able to figure out how to do the List-equivalent versions of the array operations you're currently doing. If you get stuck, post what you've got so far along with details about what's giving you trouble.
|
 |
Austin Trower
Greenhorn
Joined: Nov 06, 2012
Posts: 19
|
|
Okay I made the adjustment like this
It does not appear to have any effect and in actuality I am using the existing Array and putting into my Array List when what I need is to replace the Array with an ArrayList and I need to do it withtout changing the "Test" portion of my code further If I make the adjustment here I then will have to adjust things in the Course section and change my private String [] students to ArrayList <String> list but I need the same output as if I just used the Array.
And after that I am not sure If I will have to rearrange my getters and setters or accesor and mutator methods whichever you prefer to call them.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
Austin Trower wrote:
It does not appear to have any effect
Unless I'm missing something, it looks like all you did is add an ArrayList. You don't seem to be using it, and you don't seem to have actually replaced anything.
what I need is to replace the Array with an ArrayList and I need to do it withtout changing the "Test" portion of my code further If I make the adjustment here I then will have to adjust things in the Course section and change my private String [] students to ArrayList <String> list but I need the same output as if I just used the Array.
You need to get your requirements straight. On the one hand, you're saying "replace array with ArrayList", which suggests that everything that exposes that array publicly will also need to change to ArrayList. Then on the other hand you say, "but don't change anything that uses the array."
So you need to decide whether you're going to change, for example, to , or if you're going to leave that as it is, but then have its body construct an array from your internal List and return that.
|
 |
Austin Trower
Greenhorn
Joined: Nov 06, 2012
Posts: 19
|
|
|
My intructions are to "use an ArrayList to replace the array that stores students. I should not change the original contract of the Course class (i.e. the definition of the constructors and methods should not be changed)." That's about as clear as mud for me.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10040
|
|
The contract of the Course class means that you can't change what parameters a method takes, or the thing it returns. Basically, this;
public void addStudent(String string)
public String[] getStudents()
public int getNumberOfStudents()
public String getcourseName()
public void dropStudent(String student)
public String getStudent()
public void setStudent(String student)
So in other words, you can't all the sudden decide that your getStudents method will return a HashMap. The contract says that method must return a String[]. Nor can you all the sudden decide that your getStudent method must take an int and a boolean as a parameter.
Now, within the method body, you can do whatever you want. Nothing says you have to use a String[] inside your class, as long as when you are done, you return a String[]. If you look at the API for ArrayList, you may find a method that helps you convert it from an ArrayList into an Array...(that's a hint)
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Austin Trower
Greenhorn
Joined: Nov 06, 2012
Posts: 19
|
|
|
Thanks for clearing that up for me I will take a look and see what I can do then post again for questions or success
|
 |
Andreas Hollmann
Greenhorn
Joined: Jan 06, 2010
Posts: 27
|
|
Best Regards
Andreas Hollmann
|
 |
Austin Trower
Greenhorn
Joined: Nov 06, 2012
Posts: 19
|
|
|
got it figured out Thanks for all your help and Thanks for the welcome Jan
|
 |
 |
|
|
subject: I need to replace an array with an arraylist
|
|
|