Is this the correct way to delete an array??

J

justineee

Hi everyone,

this is a code implemented in a button in my program.
I tried to convert the array into a list to remove a specific element
then convert it back to an array.
It has no errors but when I press the delete button.. nothing
happens.. is there anything wrong with my if statements??

also, is "arrayWordsC.toArray(arrayWords);" correct? I want to convert
the List arrayWordsC to an array named arrayWords??



if (jnd.getSource()==deleteButton)
{
arrayWordsC = Arrays.asList(arrayWords);//this is a list
getInputDelete = inputDelete.getText();
if (!(getInputDelete.equals("")))
{
int dCtr;
for (dCtr = 0; dCtr < arrayWords.length; dCtr++)
{
if (arrayWordsC.get(dCtr)==getInputDelete)
{
arrayWordsC.remove(dCtr);
textArea.setText(" Welcome to E-
Dictionary v1.0\n\n");
mBox.setText("The word "+inputDelete+" has been\ndeleted in the
dictionary.");
}
}
arrayWordsC.toArray(arrayWords);
}
else
mBox.setText("a");
inputDelete.setText("");
}

thanks!
 
R

RedGrittyBrick

justineee said:
Hi everyone,

this is a code implemented in a button in my program.

Please configure your editor or IDE to use spaces for indentation and
not tabs. Tabs produce too much indentation when pasted into newsgroup
postings and make your code harder to read.

I tried to convert the array into a list to remove a specific element
then convert it back to an array.
It has no errors but when I press the delete button.. nothing
happens.. is there anything wrong with my if statements??

also, is "arrayWordsC.toArray(arrayWords);" correct? I want to convert
the List arrayWordsC to an array named arrayWords??

Array.asList() converts an array to a List. Which is the opposite of
what you say you want. To convert a List to an array use list.toArray()

Do read the API documentation for details.

Naming a List variable "arrayWords" (or "arrayWordsC") is a very bad
idea! Consider naming it "listWords" (I prefer "wordsList" but YMMV).

Since you omit the declarations of these variables I wasn't immediately
sure which is which. This is why SSCCEs are useful (see http://sscce.org).

if (jnd.getSource()==deleteButton)
{
arrayWordsC = Arrays.asList(arrayWords);//this is a list
getInputDelete = inputDelete.getText();
if (!(getInputDelete.equals("")))
{
int dCtr;
for (dCtr = 0; dCtr < arrayWords.length; dCtr++)
{
if (arrayWordsC.get(dCtr)==getInputDelete)

You should probably be using
if (arrayWordsC.get(dCtr).equals(getInputDelete)
{
arrayWordsC.remove(dCtr);
textArea.setText(" Welcome to E-
Dictionary v1.0\n\n");

I'd move that String into a constant defined outside the loop. If for
nothing else than to keep the statement on a single line of less than 80
characters.

I'm not sure why deleting a word should cause that message to be
displayed. However presumably you have a reason.

mBox.setText("The word " + inputDelete

Shouldn't that be getInputDelete? inputDelete is (presumably) a
TextField of some ilk while getInputDelete is a String?
+ " has been\ndeleted in the dictionary.");

You should probably end the for loop early here. Unless your dictionary
has duplicates there is no point searching for further ocurrences.
}
}
arrayWordsC.toArray(arrayWords);

You should assign this expression to a variable. The parameter is merely
a template for the kind of array to be created.
 
J

justineee

arrayWordsC.toArray(arrayWords);
You should assign this expression to a variable. The parameter is merely
a template for the kind of array to be created.



How?? I have my array "arrayWords".. I want it to be arrayWords still.
 
J

justineee

Hello Again Everyone,

this is the updated version..
A runtime error occurs here saying array index out of bounds..

if (jnd.getSource()==deleteButton)
{
wordsList = Arrays.asList(arrayWords);
getInputDelete = inputDelete.getText();
if (!(getInputDelete.equals("")))
{
int dCtr;
for (dCtr = 0; dCtr <= (arrayWords.length-1); dCtr++)
{
String getWord = wordsList.get(ctr);
if (getWord==getInputDelete)
{
wordsList.remove(dCtr);
textArea.setText(" Welcome to E-
Dictionary v1.0\n\n");
mBox.setText("The word "+getInputDelete+" has been\ndeleted in
the dictionary.");

}
}
arrayWords = new String [inputConfigQty-1];//I re-declared it.
inputConfigQty is the original size, I put a -1 because I deleted a
word
arrayWords = wordsList.toArray(arrayWords);//is this correct?
should I put arrayWords as a parameter? When I don't, it says
"incompatible types". arrayWords is my original array that I converted
to an ArrayList so that I can remove an element. In this line, I
wanted to bring back the updated elements in the original array,
arrayWords.

}
else
mBox.setText("a");
inputDelete.setText("");
}


Thanks RGB.
 
R

RedGrittyBrick

justineee said:
arrayWordsC.toArray(arrayWords);



How?? I have my array "arrayWords".. I want it to be arrayWords still.

If you are deleting elements, you might want a new array since arrays
can't be changed in size. You can assign a new array to the same
variable thus
letterArray = letterList.toArray(new String[letterList.size()]);

If you don't do this and the dynamic length of your list doesn't match
the declared length of your array then you'll get nulls where you
probably didn't expect them.

You may have problems deleting elements from a List that is backed by an
array - as is the case for Lists created using Arrays.asList();

I'd try to use a List throughout. I'd not converting from array to list
and back to array in order to delete an element. I'd only ever convert
to an array at the point you call some Sun or 3rd party method that only
accepts arrays and not Collections.


------------------------------------8<-------------------------------
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class ListsAndArrays {

public static void main(String[] args) {
List<String> letterList = new ArrayList<String>();
letterList.add("a");
letterList.add("b");
letterList.add("c");
letterList.add("d");
letterList.add("e");
letterList.remove(2);

String[] letterArray = new String[3];
letterList.toArray(letterArray);
printArray(letterArray);
letterArray = letterList.toArray(new String[letterList.size()]);
printArray(letterArray);


// Array backed Lists have restrictions
String[] letterArray2 = { "p", "q", "r", "s", "t" };
printArray(letterArray2);
List<String> letterList2 = Arrays.asList(letterArray2);
letterList.remove(2);
letterList.toArray(letterArray);
printArray(letterArray2);
}

static void printArray(String[] array) {
for (int i = 0; i < array.length; i++)
System.out.print(array + " ");
System.out.println();
}

}
------------------------------------8<-------------------------------
 
R

RedGrittyBrick

RedGrittyBrick said:
arrayWordsC.toArray(arrayWords);



How?? I have my array "arrayWords".. I want it to be arrayWords still.

If you are deleting elements, you might want a new array since arrays
can't be changed in size. You can assign a new array to the same
variable thus
letterArray = letterList.toArray(new String[letterList.size()]);

If you don't do this and the dynamic length of your list doesn't match
the declared length of your array then you'll get nulls where you
probably didn't expect them.

You may have problems deleting elements from a List that is backed by an
array - as is the case for Lists created using Arrays.asList();

I'd try to use a List throughout. I'd not converting from array to list
and back to array in order to delete an element. I'd only ever convert
to an array at the point you call some Sun or 3rd party method that only
accepts arrays and not Collections.


------------------------------------8<-------------------------------
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class ListsAndArrays {

public static void main(String[] args) {
List<String> letterList = new ArrayList<String>();
letterList.add("a");
letterList.add("b");
letterList.add("c");
letterList.add("d");
letterList.add("e");
letterList.remove(2);

String[] letterArray = new String[3];
letterList.toArray(letterArray);
printArray(letterArray);
letterArray = letterList.toArray(new String[letterList.size()]);
printArray(letterArray);


// Array backed Lists have restrictions
String[] letterArray2 = { "p", "q", "r", "s", "t" };
printArray(letterArray2);
List<String> letterList2 = Arrays.asList(letterArray2);
letterList.remove(2);
letterList.toArray(letterArray);
printArray(letterArray2);
}

static void printArray(String[] array) {
for (int i = 0; i < array.length; i++)
System.out.print(array + " ");
System.out.println();
}

}
------------------------------------8<-------------------------------


Oops, I was distracted and pressed enter too soon

That should be

// Array backed Lists have restrictions
String[] letterArray2 = { "p", "q", "r", "s", "t" };
printArray(letterArray2);
List<String> letterList2 = Arrays.asList(letterArray2);
letterList2.remove(2);
letterList2.toArray(letterArray2);
printArray(letterArray2);

with output

null null null
a b d e
p q r s t
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(Unknown Source)
at ListsAndArrays.main(ListsAndArrays.java:28)
 
L

Lew

arrayWordsC.toArray(arrayWords);




How?? I have my array "arrayWords".. I want it to be arrayWords still.

While that is an array, in your code sample 'arrayWordsC' is not an
array. Putting the implementation in the name is a bad thing. Use
names that reflect the domain-specific semantics, not the particular
implementation. Use of a name that contains 'array' for something
that is not an array exemplifies why it's a bad practice.
 
L

Lew

justineee said:
                        }
                        else
                                mBox.setText("a");
                        inputDelete.setText("");
                }

Please use narrower indentation. Four spaces is about the maximum for
Usenet.

Sometimes you ask questions that you've already had answered. You
might want to review the Java Tutorial on the Sun site, and there are
some good books out there that will help your Java skills also.
 
L

Lew

arrayWordsC.toArray(arrayWords);




How?? I have my array "arrayWords".. I want it to be arrayWords still.

One way:
arrayWords = arrayWordsC.toArray( new String [0] );
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top