Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I need to replace an array with an arraylist

 
Austin Trower
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Austin, Welcome to the Ranch!


Jan
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Austin Trower
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Best Regards
Andreas Hollmann
 
Austin Trower
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it figured out Thanks for all your help and Thanks for the welcome Jan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic