Arrays....

Hi all,Is it possible to remove an element from an array at a particular index, suppose i have an array which has 20 elements and i wanna remove the element at position 2 , can i remove that, please provide me an answer, thanks in advance.Bye n TC
[268 byte] By [nitin.vatsa] at [2007-11-27 4:29:56]
# 1

in a static array, no.

To do it though you need to process little bit: you need to have a new array and then search that element which you need to remove in the old array and then you ignore putting this element in the new array.

the new array size will be old array size-1.

however, in arraylist, you dont worry about these and just use remove(int index). Please check http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

hope this helps

geomana at 2007-7-12 9:39:05 > top of Java-index,Java Essentials,Java Programming...
# 2

no array api for remove

you can assign the index to null, leaving a null in the array, but you probably want the rest of the array to shift

you can manually provide the shift by creating a new array that is sized 1 smaller than the original, then copy each index from the original array into the new array, skipping the index to be removed

you may want to use Arraylist instead, it has a remove(int)

developer_jbsa at 2007-7-12 9:39:05 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks Dear...
nitin.vatsa at 2007-7-12 9:39:05 > top of Java-index,Java Essentials,Java Programming...
# 4
It is not possible.But you may use Vector instead if you want to remove elements.
eadelarosaa at 2007-7-12 9:39:05 > top of Java-index,Java Essentials,Java Programming...
# 5
> It is not possible.> > But you may use Vector instead if you want to remove> elements.Don't use Vector. Use ArrayList or LinkedList.
jverda at 2007-7-12 9:39:05 > top of Java-index,Java Essentials,Java Programming...
# 6

> Hi all,

> Is it possible to remove an element from an array at

> a particular index, suppose i have an array which has

> 20 elements and i wanna remove the element at

> position 2 , can i remove that, please provide me an

> answer, thanks in advance.

> Bye n TC

You can't remove elements from an array. You can

* Mark the element as unused, either with a special value that can't be "real" in your application or with a java.util.BitSet keeping track of used/not used.

* Copy the array's contents to a new, smaller array. (Bad idea if you'll be removing a lot of elements one by one.)

* Use a List, such as ArrayList or LinkedList. http://java.sun.com/docs/books/tutorial/collections/

jverda at 2007-7-12 9:39:05 > top of Java-index,Java Essentials,Java Programming...