• 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

Deleting element from an array - help please

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a java assesment to be in in two days. this is my first year at university and this is my first time using java also.

im having trouble with it i need to delete an element from an array but can seem to do it

could somone tell me were the input goes :$ i dont know what the varriable would be called ive not assigned one :S im so confused.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot change the size of an array; adding or removing elements simply isn't possible. All you can do is overwrite elements. You may want to check out java.util.List; List looks like an array but you can change its size.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that depending on how you define "delete" you may be able to change the specific element to "0" or "null" or something like that. While the array still has 6 (or whatever) slots, only 5 will be filled. It's not too hard to then only return the 5 that still exist.
 
Matt Watters
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ahh but you can use the Array.remove (number) fuction? all i want is to swap the number with whatever number is entered. but i cant find the name of the variable holding the input Module.delete(input) input isn't working
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For what you want to do, you'll need to make your own method. Method will take an array and index witch you wish to delete and it returns a new array.

 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Martin's solution is a good idea, but if your requirements don't specifically mention using an array, I would just use an ArrayList to make it easier.

http://download-llnw.oracle.com/javase/6/docs/api/java/util/ArrayList.html

Hunter
 
Matt Watters
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, That helped alot . One last question, in the above, What is the variable that is assigned the input? i dont have a clue were the inputs assigned to
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think in this case you might use a console object.



An issue might also arise with the braces in the program. You should look at those and make sure methods only one beginning and one ending brace.
 
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic