PiRatCollective Posted October 21, 2005 Share Posted October 21, 2005 Is it possible to delete array element in Java? or do I have to copy the Array element by element? Link to comment Share on other sites More sharing options...
0 Joseph Zollo Posted October 21, 2005 Share Posted October 21, 2005 Yes, just set it equal to null and let the garbage collector do the rest. Link to comment Share on other sites More sharing options...
0 ItchyAnus Posted October 21, 2005 Share Posted October 21, 2005 If you're using an array with a set size like String[5], then you cannot delete an element and expect the array to have a length of 4. Setting an array element to null in this case just assigns that array element to have a null value. This could cause problems if you were iterating through it and didn't consider null values. If you have to use an array, then you would have to copy the entire array to some other array, minus the element you don't want. If you're using a dynamic list like ArrayList or LinkedList, then you can call a method to delete an element that will cause the length of the array to decrease by one. It sounds like you should use a dynamic list. The one that resembles an array the most is ArrayList. Link to comment Share on other sites More sharing options...
0 PiRatCollective Posted October 21, 2005 Author Share Posted October 21, 2005 Ok let's say I don't know how to use an ArrayList and will figure out how to later, so I'm going to copy it into a temporary Array and then copy it back. If I set a larger array equal to a smaller array of the same type will it also set the size of the array? Link to comment Share on other sites More sharing options...
0 ItchyAnus Posted October 21, 2005 Share Posted October 21, 2005 Setting a larger array to a smaller array only 'replaces' it: int[] a = new int[10]; int[] b = new int[5]; b = a; // results in b and a referring to the same int[10]. int[5] is gone. What you probably want is something like this: // Comments omitted so you can figure out how it works. =) int[] a = new int[10]; int[] b = new int[9]; int i = 0; int j = 0; int elementToDelete = 5; while (i < a.length && j < b.length) { if (i == elementToDelete) { i++; } else { b[j] = a[i]; i++; j++; } } // At this point, b is equal to a without the 5th element Link to comment Share on other sites More sharing options...
0 karma_police Posted October 21, 2005 Share Posted October 21, 2005 Just use an ArrayList, javadoc is all you need to learn how to use it. Link to comment Share on other sites More sharing options...
0 sokoman Posted October 22, 2005 Share Posted October 22, 2005 To delete an item in an array, first you have to find the item. Here's a simple method. public static void main(String[] a) { int[] arr = new int[4]; int i, size = 0, searchItem; arr[0] = 23; arr[1] = 12; arr[2] = 5767; size = 3; searchItem = 23; for(int d = 0; d < size; d++) System.out.print(arr[d]+" "); System.out.println(); for(i = 0; i < size; i++) if(arr[i] == searchItem) break; for(int k = i; k < size; k++) arr[k] = arr[k+1]; size--; for(int d = 0; d < size; d++) System.out.print(arr[d]+" "); System.out.println(); } Link to comment Share on other sites More sharing options...
0 _kane81 Posted October 23, 2005 Share Posted October 23, 2005 just reiterating, yes you need to create a new array when you delete an item. basically you copy all the elements for the old array into the new one except the item that is being deleted. sound like a lot of work to delete an element, but that is what you have to do. Or else you use a Vector or ArrayList, which do this for you. Link to comment Share on other sites More sharing options...
0 panchonb Posted October 23, 2005 Share Posted October 23, 2005 just reiterating, yes you need to create a new array when you delete an item.basically you copy all the elements for the old array into the new one except the item that is being deleted. sound like a lot of work to delete an element, but that is what you have to do. Or else you use a Vector or ArrayList, which do this for you. 586712019[/snapback] Actually Vector and ArrayList will just move all the elements that have indices greater that the deleted element up one. If you actually want to clear the memory and not have "empty" elements, you would call ArrayList.trimToSize(). The reason is for performance: copying an array can take a bit of time depending on the amount of elements and where the deletion took place. By having a trimToSize() function the developer is given more control over memory/performance. just my two cents. Link to comment Share on other sites More sharing options...
0 _kane81 Posted October 23, 2005 Share Posted October 23, 2005 ^ interesting, never knew that. never really thought about it to be honest. Link to comment Share on other sites More sharing options...
0 _kane81 Posted October 24, 2005 Share Posted October 24, 2005 ^ I believe "System.arraycopy" does something similar to what I was saying. maybe my wording was really incorrect "copy", and "new" are a bit misleading? taken from Sun's ArrayList implementation /** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to removed. * @return the element that was removed from the list. * @throws IndexOutOfBoundsException if index out of range <tt>(index * < 0 || index >= size())</tt>. */ public Object remove(int index) { RangeCheck(index); modCount++; Object oldValue = elementData[index]; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work return oldValue; } /** * Copies an array from the specified source array, beginning at the * specified position, to the specified position of the destination array. * A subsequence of array components are copied from the source * array referenced by <code>src</code> to the destination array * referenced by <code>dest</code>. The number of components copied is * equal to the <code>length</code> argument. The components at * positions <code>srcPos</code> through * <code>srcPos+length-1</code> in the source array are copied into * positions <code>destPos</code> through * <code>destPos+length-1</code>, respectively, of the destination * array. * <p> * If the <code>src</code> and <code>dest</code> arguments refer to the * same array object, then the copying is performed as if the * components at positions <code>srcPos</code> through * <code>srcPos+length-1</code> were first copied to a temporary * array with <code>length</code> components and then the contents of * the temporary array were copied into positions * <code>destPos</code> through <code>destPos+length-1</code> of the * destination array. * <p> * If <code>dest</code> is <code>null</code>, then a * <code>NullPointerException</code> is thrown. * <p> * If <code>src</code> is <code>null</code>, then a * <code>NullPointerException</code> is thrown and the destination * array is not modified. * <p> * Otherwise, if any of the following is true, an * <code>ArrayStoreException</code> is thrown and the destination is * not modified: * <ul> * <li>The <code>src</code> argument refers to an object that is not an * array. * <li>The <code>dest</code> argument refers to an object that is not an * array. * <li>The <code>src</code> argument and <code>dest</code> argument refer * to arrays whose component types are different primitive types. * <li>The <code>src</code> argument refers to an array with a primitive * component type and the <code>dest</code> argument refers to an array * with a reference component type. * <li>The <code>src</code> argument refers to an array with a reference * component type and the <code>dest</code> argument refers to an array * with a primitive component type. * </ul> * <p> * Otherwise, if any of the following is true, an * <code>IndexOutOfBoundsException</code> is * thrown and the destination is not modified: * <ul> * <li>The <code>srcPos</code> argument is negative. * <li>The <code>destPos</code> argument is negative. * <li>The <code>length</code> argument is negative. * <li><code>srcPos+length</code> is greater than * <code>src.length</code>, the length of the source array. * <li><code>destPos+length</code> is greater than * <code>dest.length</code>, the length of the destination array. * </ul> * <p> * Otherwise, if any actual component of the source array from * position <code>srcPos</code> through * <code>srcPos+length-1</code> cannot be converted to the component * type of the destination array by assignment conversion, an * <code>ArrayStoreException</code> is thrown. In this case, let * <b><i>k</i></b> be the smallest nonnegative integer less than * length such that <code>src[srcPos+</code><i>k</i><code>]</code> * cannot be converted to the component type of the destination * array; when the exception is thrown, source array components from * positions <code>srcPos</code> through * <code>srcPos+</code><i>k</i><code>-1</code> * will already have been copied to destination array positions * <code>destPos</code> through * <code>destPos+</code><i>k</I><code>-1</code> and no other * positions of the destination array will have been modified. * (Because of the restrictions already itemized, this * paragraph effectively applies only to the situation where both * arrays have component types that are reference types.) * * @param src the source array. * @param srcPos starting position in the source array. * @param dest the destination array. * @param destPos starting position in the destination data. * @param length the number of array elements to be copied. * @exception IndexOutOfBoundsException if copying would cause * access of data outside array bounds. * @exception ArrayStoreException if an element in the <code>src</code> * array could not be stored into the <code>dest</code> array * because of a type mismatch. * @exception NullPointerException if either <code>src</code> or * <code>dest</code> is <code>null</code>. */ public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); Link to comment Share on other sites More sharing options...
Question
PiRatCollective
Is it possible to delete array element in Java? or do I have to copy the Array element by element?
Link to comment
Share on other sites
10 answers to this question
Recommended Posts