aspose file tools*
The moose likes Beginning Java and the fly likes Deleting element from an array - help please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Deleting element from an array - help please" Watch "Deleting element from an array - help please" New topic
Author

Deleting element from an array - help please

Matt Watters
Greenhorn

Joined: Aug 02, 2010
Posts: 4
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matt Watters
Greenhorn

Joined: Aug 02, 2010
Posts: 4
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
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
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.



SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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


"If the facts don't fit the theory, get new facts" --Albert Einstein
Matt Watters
Greenhorn

Joined: Aug 02, 2010
Posts: 4
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
Enrique Gonzalez Moore
Greenhorn

Joined: Jun 12, 2010
Posts: 7
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Deleting element from an array - help please