Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
How can I test to see if something was removed from my ArrayList.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please post the code for your test class so we can see what you are doing
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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?

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Locking this thread, since the OP created this new thread for his most recent question.
 
    Bookmark Topic Watch Topic
  • New Topic