removing an element from String array in android

49,123

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);
Share:
49,123
Madhumitha
Author by

Madhumitha

Updated on July 09, 2022

Comments

  • Madhumitha
    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
      Mikhaili over 11 years
      You can convert it to arraylist
    • Mikhaili
      Mikhaili over 11 years
      if you need something more general stackoverflow.com/a/122207/619713
    • Mikhaili
      Mikhaili over 11 years
      Optimization Parse one time current_id to string and equal it to arr_fav[i]
  • amit
    amit over 11 years
    -1 for "a List which is dynamic". The List interface does not require it being dynamic, it can even be immutable.
  • Brian Roach
    Brian Roach over 11 years
    THank 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
    Ram kiran Pachigolla over 11 years
    this is not exact answer. as he is checking the current_id with in array.
  • The111
    The111 over 11 years
    It 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
    amit over 11 years
    At 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
    The111 over 11 years
    No, it is not an exact answer. It's an illustration how to use the remove() method of ArrayList, clearly.
  • Ram kiran Pachigolla
    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
    Brian Roach over 11 years
    Dear 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
    The111 over 11 years
    Yes, but the only feature of the ArrayList you're using is add(). 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 the ArrayList functionality.
  • Mikhaili
    Mikhaili over 11 years
    it's internal class com.android.internal.util.ArrayUtils
  • Mikhaili
    Mikhaili over 11 years
    Ah Ok but i think he develops for Android
  • Nirav Tukadiya
    Nirav Tukadiya over 11 years
    check out my code.if you have more element you should use variables for swapping elements.
  • Mikhaili
    Mikhaili over 11 years
    1. arr_fav is string array and you remove integer
  • xagyg
    xagyg over 11 years
    Thanks for the tip. Updated to use String.valueOf.
  • Brian Roach
    Brian Roach over 11 years
    @Madhumitha - Yes it "works", but you really should read the above comment.