Deleting an element in an array..

J

justineee

Hi Everyone,

I am making a project that runs like a word storage. The user inputs
words and stores it in arrays. Also, the user can delete words and
display the new collection of words. However, I am having problems in
deleting an element in an array.

Here is my code.

if (jnd.getSource()==deleteButton && (!(inputDelete.equals(""))))
{

getInputDelete = inputDelete.getText();
int dCtr;
List<String> arrayWordsCopy = Arrays.asList(arrayWords);
for (dCtr = 0; dCtr < arrayWords.length; dCtr++)
{
if (getInputDelete.equalsIgnoreCase(arrayWords[dCtr]))
{
arrayWordsCopy.remove(dCtr);
textArea.append("The word "+getInputDelete+" has been deleted in
the dictionary.");
}
else
{
}
}
arrayWordsCopy.toArray(arrayWords);
}

I tried to convert the array to List temporarily so I can use the
".remove" method. Then I will get it back to an array so the element
will be deleted permanently. There is no error in this code when I
compile it but when I run the program and I press the button
"deleteButton", there is a run-time error the occurs.

Can anyone please help me to delete an element in an array?
 
M

Martin Gregorie

I am making a project that runs like a word storage. The user inputs
words and stores it in arrays. Also, the user can delete words and
display the new collection of words. However, I am having problems in
deleting an element in an array.
Why are you comparing against the array instead of the List you just
built?

More to the point, why bother with the complexity and overheads of
continually converting a array to a List and back? Why aren't you using
an ArrayList or TreeSet instead? ArrayList lets you control ordering and
duplicates while TreeSet will do both for you. Both provide add/remove/
existence tests.
 
A

Andreas Leitgeb

Lew said:
To delete element 'kx' (>= 0 and less than the current number of active
elements) from an array 'stuff' that initially holds 'stuffLength' elements:
--stuffLength;
for ( int jx = kx; jx < stuffLength; ++jx )
{
stuff [jx] = stuff [jx+1];
}
stuff [stuffLength] = null;
Pretty straightforward, yes?

In case ordering doesn't matter, there's also this simpler approach:
stuff[kx]=stuff[--stuffLength]; stuff[stuffLength]=null;

Anyway, using java.util.ArrayList (or any of the sorting Collections)
is most likely the better solution.
 
R

Roedy Green

I am making a project that runs like a word storage. The user inputs
words and stores it in arrays. Also, the user can delete words and
display the new collection of words. However, I am having problems in
deleting an element in an array.

The easy way is to use an ArrayList, and let remove figure out how to
do the sliding.

Strictly speaking you can't delete an element. To have a smaller
array you need to allocate a new array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top