aspose file tools*
The moose likes Beginning Java and the fly likes How To Delete An Element From Array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How To Delete An Element From Array?" Watch "How To Delete An Element From Array?" New topic
Author

How To Delete An Element From Array?

harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
hello,everyone as i am stuck to this point how to delete the element from an array
in my case i already sorted an array in ascending order and i want to delete the lowest and highest element from an array .
how To Do this???
pardon me if this is basic question.
thank you in advance
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You can't change the size of an array once it's created. Your choices are:

1. Create a new array with the desired length and copy in the desired elements from the original.

2. Use separate variables to keep track of which elements are and are not used.

3. Put a special value that can't occur in your real data in the unused elements to indicate that they're unused, such as null or -1.

4. Use a List instead of an array, so that it can handle the grunt work for you.
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
it will be really helpful if you give me some example on how to do it i tried searching on internet but not understanding those things.and i have to use arrays i am not allowed to use list
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

harshal deshpande wrote:it will be really helpful if you give me some example on how to do it


I already outlined a couple of approaches. Since you're new to arrays and trying to learn, you need to get some practice and struggle through this stuff as best you can.

Take your best shot, and when you get stuck, post again with a more specific question about exactly what you're having trouble with.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

harshal deshpande wrote:it will be really helpful if you give me some example on how to do it i tried searching on internet but not understanding those things.and i have to use arrays i am not allowed to use list


Here is how the pesudo code would look for Jeff's case 1 above
1. Create a new array with the desired length and copy in the desired elements from the original.

1) Inspect existing sorted array
2) Identify the length
3) Create a new array whose length is less two elements
4) Inspect each element from the original array, check if it is required or not, if required copy to new array
5) Continue till all elements in the original array are inspected.

@Harshal,
Now can you write such pseudo code for Jeff's other approaches? Once you have the pseudo code, it will be easier to convert to real code.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
thanks to everyone,i did tried one example for copying one int[] into other and removing lowest and highest of them and that is working correctly,thanks i will go further with that problem if i get any more doubt i know you are there to help me thank you once again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Well done
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Maneesh Godbole wrote:
Here is how the pesudo code would look for Jeff's case 1 above
...
4) Inspect each element from the original array, check if it is required or not, if required copy to new array
5) Continue till all elements in the original array are inspected.


Or, if it's known that you always need exactly all but the first and last, then just write your loop to copy the specific elements needed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How To Delete An Element From Array?