Reputation: 129
Forgive the clunky title
I want to write a method that removes a specific entry from an array, but doesn't leave a null gap in the array. For example if a String array contained
|aa,bb,cc,dd,ee|
the user would be prompted to enter which number they wanted removed, the method would find the index of that entry, remove that index, then move the null entry to the last slot.
So if the user entered cc, the array's contents would be
|aa,bb,dd,ee,null|
EDIT: I realized I left out some information here. the entry I'm looking to remove will be passed from another method. I will then use a for loop to find the index of the entry (If not found nothing is done). However I'm stuck on how to do the deletion.
Upvotes: 3
Views: 1497
Reputation: 10959
One option would be to make the array into a List, remove the entry then make it into an array again.
ArrayList<String> aList = new ArrayList<String>(Arrays.asList(yourArray));
aList.remove(yourIndex);
return aList.toArray(new String[yourArray.length]);
As a Test example
public static void main(String[] args) {
String[] yourArray = new String[]{"FF","AA","BB"};
for(String s : yourArray)
System.out.println(s);
System.out.println("");
ArrayList<String> aList = new ArrayList<String>(Arrays.asList(yourArray));
aList.remove(1);
String[] r = aList.toArray(new String[yourArray.length]);
for(String s : r)
System.out.println(s);
}
Output
FF
AA
BB
FF
BB
null
Upvotes: 0
Reputation: 76
You have the gist of it in your description
the user would be prompted to enter which number they wanted removed, the method would find the index of that entry, remove that index, then move the null entry to the last slot.
with not writing the actual code, since this seems like a school assignment, I'll just add what happens to the indexes of the rest of the entries after the deleted index?
Upvotes: 0
Reputation: 3822
I would do it like this maybe:
for (int i = index; i < array.length -1; i++) {
array[i] = array[i+1];
}
array[array.length - 1] = null;
Upvotes: 2
Reputation: 44449
First of all: I strongly suggest to use an ArrayList
where you can easily remove and add items without having to change the rest of the collection (it also has a toArray()
method).
That being said, tihs would be a sample solution to do it with just arrays:
public static void main(String[] args) {
String[] arr = new String[5];
arr[0] = "aa";
arr[1] = "bb";
arr[2] = "cc";
arr[3] = "dd";
arr[4] = "ee";
System.out.println(Arrays.toString(arr));
int deleteIndex = 2;
System.arraycopy(arr, deleteIndex + 1, arr, deleteIndex, arr.length - deleteIndex - 1);
arr[4] = null;
System.out.println(Arrays.toString(arr));
}
Output:
[aa, bb, cc, dd, ee]
[aa, bb, dd, ee, null]
The idea is to to shift the elements one place ahead starting from the index to delete + 1. Afterwards you set the latest item manually to null
or it will duplicate the last entry.
Upvotes: 6
Reputation: 54074
To keep the contents in order as you remove you need to shift elements to the left of the array.
The most naive and wasteful way would be to initialize another array with 1 size less than the current.
Then copy all the elements in a loop from your array but not the one that the user wants to remove (jumping that index)
Upvotes: 0