• Post Reply 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

How to code for deleting and finding an item on a list?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is for a class of mine and the teacher didn't really explain how to code for this assignment except for how to add items. I am supposed to make a listing program that will add an item, remove an item, finding an item on the list and display the list. I can't even begin on how to remove items or finding the items. Any help is appreciated.

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Looking at your methods, I see you are an array. Is this really necessary since you are meant to work with java.util.List? Yet lists and arrays can be interchanged using the List.toArray() and Arrays.asList() methods.

In the java.util.List interface, there are 2 ways to "add": add(element) and add(position, element). The first one adds the element to the end of the list and returns a boolean. The latter adds the specific element to the position you specify (eg 0=first element, n-1=last element, n=some where in between) and return nothing (void).

To remove, again are 2 methods: remove(position) and remove(element). The first returns the removed element where the other returns a boolean.

To search a list? You may use contains(element) method that returns a boolean.
 
Sid Stephens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where would I put the code for remove? I'm confused by that as well.
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well given your program is user-controlled, meaning you should let the user add/remove/search items in the list. Probably have some pre-populated items.

Say there are already items in the list. Your options will be a) add new item, b) remove existing item or c) search an item.

I shall let you figure out how to get inputs from users (hint java.util.Scanner).
 
Sid Stephens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I got so far. How do I get use the remove (position) method? It's not clicking in my head.


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sid Stephens wrote:This is what I got so far. How do I get use the remove (position) method? It's not clicking in my head.



Here is your insert method...

Sid Stephens wrote:



Notice how it works? You move a range of elements to create a hole in the array, and then you write the new element into the hole. To delete, you need to do the opposite, you need to move a range of elements to close up that hole. You don't actually need to remove the element, as the range move will also replace the element to be removed.

Henry
 
Sid Stephens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now the problem is I can't see the list. I'm not sure what I've done.

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sid Stephens wrote:Now the problem is I can't see the list. I'm not sure what I've done.



How did your insert method get access to the list? Well, your remove method can get access the same way.

Henry
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote: . . . To delete, you need to do the opposite, you need to move a range of elements to close up that hole. . . .

But there is something Henry forgot to mention.
You have to set the very last element in the array to null, otherwise you can fill your array with copies of the last item added. In a large application that might cause a memory leak.

You are talking about Lists but displaying arrays. Which are you supposed to be using? Does that class represent a List? If so, why are all the methods static and why isn't the array a field?

And welcome again
 
Sid Stephens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I restarted the thing from the beginning and I think I just made it worse. I'm not sure how I messed it up even more.


 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't try to do so much all at once.

Let's have an object‑oriented solution. Get 99% of that code out of the main method. Choose a different name for your class from List (unless you have been told specifically to call it List). Your main method will now look something like this:-You will need the works in a class called ListArray (or something) and you test it in that run() method. You might have a ListArray object as a field of the class I just showed you or as a local variable in run(). Doesn't matter too much which you choose, but if it is a field you will need to write a constructor to initialise it.

Get add working. That is probably the simplest operation, so get that working first and come back to the other operations later.
Find out about the switch statement as an example of selection. Find out about the use of Strings in a switch statement (Java7+ only).
 
Greenhorn
Posts: 14
MySQL Database Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your new code you have:

Surely it should be:

OR (||) instead of AND (&&)
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still think it should be changed to a switch, but good catch
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your list class should have an insert at top method public boolean insertAtTop(Item i) and you return true if it worked (or change to void method).
 
Sid Stephens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I still think it should be changed to a switch, but good catch



Thanks Campbell. You gave me the idea to stick with what I know for some of the commands and go with integers. I didn't use switches but I got it to work. I still can figure out how to make a method for removing an item off of the list. Also thanks to everybody else who've tried to help so far.


 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have lots of repeated code in those ifs. You should have a method which you call from each option.
And why have you marked all your methods static?
 
Sid Stephens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You have lots of repeated code in those ifs. You should have a method which you call from each option.
And why have you marked all your methods static?




For the static, that's the only way the teacher taught us to do.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic