removing an element from String array in android
Solution 1
its better to use arraylist
arr_fav = {"1","2","3"};
List<String> numlist = new ArrayList<String>();
for(int i= 0;i<arr_fav.length;i++)
{
if(current_id == Integer.parseInt(arr_fav[i]))
{
// No operation here
}
else
{
numlist.add(arr_fav[i]);
}
}
arr_fav = numlist .toArray(new String[numlist .size()]);
Solution 2
You don't.
Arrays can not be resized.
You would need to create a new (smaller) array, and copy the elements you wished to preserve into it.
A better Idea would be to use a List
implementation that was dynamic. An ArrayList<Integer>
for example.
Solution 3
Arrays in Java are not dynamic, you can use an ArrayList
instead.
Solution 4
You can copy the array elements that you want into a new array
j = 0;
for(int i= 0;i<arr_fav.length;i++)
{
if(current_id != Integer.parseInt(arr_fav[i]))
{
arr_new[j++] = arr_fav[i];
} }
Solution 5
Use an ArrayList
instead of an array. It supports features like deleting any element, dynamic size, and many more.
ArrayList<String> arr_fav_list = new ArrayList<String>();
arr_fav_list.addAll(arr_fav);
arr_fav_list.remove(1);
Comments
-
Madhumitha almost 2 years
Am doing a simple android application.In that I am deleting an element from array using the following code.
arr_fav = {"1","2","3"}; for(int i= 0;i<arr_fav.length;i++) { if(current_id == Integer.parseInt(arr_fav[i])) { arr_fav[1] = null; } }
By doing this am getting the array like arr_fav = {"1",null,"3"}.But I want like arr_fav = {"1","3"}.How to delete an element.Am new to this android development.Please help me to solve this.
-
Mikhaili over 11 yearsYou can convert it to arraylist
-
Mikhaili over 11 yearsif you need something more general stackoverflow.com/a/122207/619713
-
Mikhaili over 11 yearsOptimization Parse one time current_id to string and equal it to arr_fav[i]
-
-
amit over 11 years-1 for "a
List
which is dynamic". TheList
interface does not require it being dynamic, it can even be immutable. -
Brian Roach over 11 yearsTHank you Mr. pedantic. I guess "of some sort which would be dynamic" wasn't clear enough? Nevermind, I'll edit it just for you.
-
Ram kiran Pachigolla over 11 yearsthis is not exact answer. as he is checking the current_id with in array.
-
The111 over 11 yearsIt is indeed better to use an array list, but the way you're using it does not utilize any of its advantages. In your code above,
numlist
could be an array. -
amit over 11 yearsAt least be honest. A previous version of the answer was exactly as I cited, and not "that would be..." if you later edited it, it's a different story. Long story short - my comment +downvote did exactly what it should have been - made you change the answer to something more accurate.
-
The111 over 11 yearsNo, it is not an exact answer. It's an illustration how to use the
remove()
method ofArrayList
, clearly. -
Ram kiran Pachigolla over 11 years@The111, Here i am using arraylist instead of resizing array, as java does not provide resizing of its array by default. please check it carefuylly
-
Brian Roach over 11 yearsDear lord, I was editing when you commented, which was about 30 seconds after I posted the revision you're complaining about. Nevermind that without trying to be Ranger Rick you could simply have said "Hey, that's not exactly correct".
-
The111 over 11 yearsYes, but the only feature of the
ArrayList
you're using isadd()
. If you're just going to add things one by one to the end of a list, you can use a simple array. The OP wanted to remove something from the middle of a list. Iterating through the entire list and putting everything but the deleted item into a new list is not a solution that requires or takes advantage of theArrayList
functionality. -
Mikhaili over 11 yearsit's internal class com.android.internal.util.ArrayUtils
-
Mikhaili over 11 yearsAh Ok but i think he develops for Android
-
Nirav Tukadiya over 11 yearscheck out my code.if you have more element you should use variables for swapping elements.
-
Mikhaili over 11 years1. arr_fav is string array and you remove integer
-
xagyg over 11 yearsThanks for the tip. Updated to use String.valueOf.
-
Brian Roach over 11 years@Madhumitha - Yes it "works", but you really should read the above comment.