Note: Following is my homework/assignment, feel free not to answer if you will.
I want to delete/remove an element from an String array(Set) basic, I'm not allowed to use Collections..etc.
Now I have this:
void remove(String newValue) {
for ( int i = 0; i < setElements.length; i++) {
if ( setElements[i] == newValue ) {
setElements[i] = "";
}
}
}
I does what I want as it remove the element from an array but it doesn't shorten the length. The following is the output, basically it remove the element indexed #1.
D:\javaprojects>java SetsDemo
Enter string element to be added
A
You entered A
Set size is: 5
Member elements on index: 0 A
Member elements on index: 1 b
Member elements on index: 2 hello
Member elements on index: 3 world
Member elements on index: 4 six
Set size is: 5
Member elements on index: 0 A
Member elements on index: 1
Member elements on index: 2 hello
Member elements on index: 3 world
Member elements on index: 4 six
Basically you need to create a new array which is as long as the old array's length minus 1 and then you need to copy the valid elements from the old to the new array in a loop and then replace the old array with the new array.
Since this is
homework
, details are left away. Feel free to post a comment for a bit more clarification.The size of an array in Java can't be changed once the array is created. The following links should help you with transferring the existing items to a new array :-)
See: System.arraycopy and Array.copyOf(*).
All that
setElements[i] = "";
does is change the value of an element in the array. It does not actually remove anything from the array. If you were using a collection class and calledremove(i)
on it, then you would actually be removing that element from the collection. But here, you're just changing its value. However, arrays in Java have a fixed size and cannot be resized, so there is no way to remove elements from them. The solution, therefore, is to create a new array with a length one shorter than the old one, and then copy all of the values that you want to keep into the new one. So,Create new array with a length of
setElements.length - 1
.Copy all of the elements in
setElements
into the new array, except for the one which you're looking to remove. Be careful of the fact that the indices into the two arrays will be off by one rather than equal once you've reached the index for the element that you wish to remove.Set
setElements
to the new array if you want to keep using the same variable for your array.You can't change the length of an array object once it's created. Here's an excerpt from JLS 10.2. Array Variables:
This means that for this problem, you'd have to allocate a new array that's one-element shorter than the original array, and copy over the remaining elements.
If you need to remove element at index
k
, and the original array hasL
elements, then you need to copy over elements (upper bounds are exclusive):[0,k)
to[0,k)
(k
elements)[k+1,L)
to[k,L-1)
(L-k-1
elements).L-1
elements copiedThis uses
System.arraycopy
; you can always write your own if this isn't allowed.This is a simplistic implementation that doesn't handle
src == dst
, but it's sufficient in this case.See also
Note on
==
forString
comparisonMost of the time, using
==
to compareString
objects is a mistake. You should useequals
instead.See also