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

delete an element from an array

mark donner
Greenhorn

Joined: Oct 08, 2011
Posts: 17
This is a practice example for my test that i cannot figure out. Please help

Question:
Write a method deleteElement which takes as input an int[] and an int target and deletes all
occurrences of target from the array. The method should return the new int[]. Question to consider:
Why is it that we have to return an array and can't simply change the input parameter array?

this is the code that ive come up with but I know its wrong. The problem with it is that it does not take into consideration whether or not the array[i+1] is equal to the target number or not.

Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Question:
Write a method deleteElement which takes as input an int[] and an int target and deletes all
occurrences of target from the array. The method should return the new int[]. Question to consider:
Why is it that we have to return an array and can't simply change the input parameter array?


Array size is constant and so cannot be changed. You cannot remove an element from an array. you can modify it though. So you have to create a new Array and put all the elements except the target element and return this new array. Where as with array lists or any lists you can return the input parameter array list by removing the element using .remove() method. The size of list changes dynamically.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
I think you mean in an array of primitives, you can only replace an element.
I am changing your code tags to quote, which is more appropriate.
mark donner
Greenhorn

Joined: Oct 08, 2011
Posts: 17
I didnt realize what an array list was. I thought it was the exact same thing as an array. Im gonna work on my code and I think I will have a better solution.
mark donner
Greenhorn

Joined: Oct 08, 2011
Posts: 17
How do you create an array list?

I read that the array list initializes with a value of 10. Is this true?

thanks
mark donner
Greenhorn

Joined: Oct 08, 2011
Posts: 17
Campbell Ritchie,

What do you mean that you are changing code tags to quotes?

Im new this forum so I want to understand how it works.

Thank you
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

I believe Campbell was talking to Harsha Smith. I didn't see it, but I'm guessing that post originally hay the quotation you see now in 'code' tags, which would just look odd.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Yes, Fred, HS had used code tags, where quote would have been more appropriate. Sorry for any misunderstanding.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
mark donner wrote:How do you create an array list?

I read that the array list initializes with a value of 10. Is this true?

thanks


Mark, you can find the JavaDoc for ArrayList here.

ArrayList is a concrete implementation of the List interface, so if you want to follow best practices, you would probably want to do something like this:

where Type is a class type (that is, a non-primitive).

To answer your second question, yes, unless you specify a different initial capacity, an ArrayList will have the default initial capacity of 10.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: delete an element from an array