This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deleting element from an array - help please

 
Matt Watters
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • 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.

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Enrique Gonzalez Moore
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic