This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

ArrayList

Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
How can I test to see if something was removed from my ArrayList.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
It depends on what exactly you mean:
If you want to see if the object you are trying to remove was actually in the list and has been removed then check the value returned by remove().
If you just want to see if something has been removed compare the size of the list before and after the remove.
If you want to see if a specific object is still in the list you could possibly call contains() passing in the object to see if it is still there. Of course this uses the equals() method so the effectiveness of this may depend so depending on whether it has been overridden or not.

Or is it something else altogether.
Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
Yes this what I am looking for:

If you want to see if the object you are trying to remove was actually in the list and has been removed then check the value returned by remove().

Can you give me an example of the code that would go in a tester class? I think I would call the class along with the remove the method?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Zachary T. Anderson wrote:
If you want to see if the object you are trying to remove was actually in the list and has been removed then check the value returned by remove().

Can you give me an example of the code that would go in a tester class? I think I would call the class along with the remove the method?


For your test class, 1) Create an List. 2) Add some stuff to the List. 3) Call remove() to remove something that's there. Print out the result. Compare it to what you expect. 4) Call remove() to remove something that's not there. Print out the result. Compare it to what you expect.

Give it your best shot, and if you get stuck, ask a more specific question about the bit that's giving you trouble.
Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
I have tried



Where Student is the class, removeGrade is the method, grades is the ArrayList, and findGrade is the object it looked for and removed.
I put this in my tester put it doesn't work. I get the following error.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Where Student is the class, removeGrade is the method, grades is the ArrayList, and findGrade is the object it looked for and removed.

Well shouldn't you be calling the removeGrade() method on an instance of Student. Why would you call a method called findGrade when that is the object you want to remove?
Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
Okay that makes sense. But still confused on how the code would look compared to mine.

So I would put in my tester:
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
It's hard to say after looking at a single line of code.
What is 'Grades', if it references an instance of the Student class then yes but why is it called 'Grades' and not 'student'. If it is your grades array then no.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Zachary T. Anderson wrote:Okay that makes sense. But still confused on how the code would look compared to mine.

So I would put in my tester:


No idea.

What does getRemoveGrade() do?

However, given the problems you're having so far, I would strip away some fluff and test something simpler.

Forget your getRemoveGrade() method. Forget whatever you're putting in your list. Start by just building a List and adding some Strings to it, trying some List.remove() calls for both present and absent Strings, and make sure you understand why you get the results you do.

Then move on to putting in the List whatever you'll really be putting in there--your Grade class or whatever it will be. Do the same test. Make sure you've got your equals() method written properly, so the List can correctly determine whether an object is present. Just simple direct List add() and remove() calls.

Then once that works, you can fit those calls into whatever methods in your app will be managing the adds and deletes for you.
Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
It was suppose to be Students and I changed it and it still didn't work. What the program does is gets students grades and removes all that got "B".
I know the program is doing it in the tester because they are removed from the print out. But the instructions state to print them out after the
grades that were not removed. For some reason when I call it I keep getting that error. That it can't find symbol.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Please post the code for your test class so we can see what you are doing
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tony Docherty wrote:Please post the code for your test class so we can see what you are doing


And for the class that you're testing.
Zachary T. Anderson
Greenhorn

Joined: Mar 12, 2013
Posts: 16
Here is one of problems I am having:





I have three classes: Student, Grade, and StudentTester. This code is in Student. It is calling the scores array from Student. I think this works.
What I would like to do is call the getLowestGrade method into my StudentTester, but keep getting the error:



So my question is first is my code correct for the lowestGrade and second how can I call this correctly in my tester?

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7521
    
  18

Zachary T. Anderson wrote:So my question is first is my code correct for the lowestGrade and second how can I call this correctly in my tester?

Zachary,

1. The answer to your original question ("How can I test to see if something was removed from my ArrayList") is: you don't.
ArrayList has been around for a very long time; so if there was any problem with its remove(Object) method, someone would have come up with issues long before you.

2. Your thread has now drifted onto wider questions, and we still don't have enough information to help you. Furthermore, your last post suggests a compilation problem, which has nothing to do with your code not working; it says that what you've written is not legal Java.

3. READ the message you're getting. What do you think "non-static method getLowestGrade() cannot be referenced from a static context" means?

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Locking this thread, since the OP created this new thread for his most recent question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList
 
Similar Threads
Arraylist Vs LinkedHashMap
readonly ArrayList
Array of object in phonebook
How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content
why some classes are called deprecated & some legacy?