10

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.

G M Ramesh
  • 3,420
  • 9
  • 37
  • 53
Madhumitha
  • 3,794
  • 8
  • 30
  • 45

14 Answers14

20

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()]);
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
  • 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. – The111 Dec 06 '12 at 08:48
  • @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 – Ram kiran Pachigolla Dec 06 '12 at 08:50
  • 1
    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. – The111 Dec 06 '12 at 08:52
  • @Madhumitha - Yes it "works", but you really should read the above comment. – Brian Roach Dec 06 '12 at 16:09
10

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.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • 1
    -1 for "a `List` which is dynamic". The `List` interface does not require it being dynamic, it can even be immutable. – amit Dec 06 '12 at 08:45
  • 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. – Brian Roach Dec 06 '12 at 08:46
  • 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. – amit Dec 06 '12 at 08:48
  • 3
    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". – Brian Roach Dec 06 '12 at 08:50
5

Arrays in Java are not dynamic, you can use an ArrayList instead.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
2

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];
 } }
Fyre
  • 1,180
  • 8
  • 12
2

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);
The111
  • 5,757
  • 4
  • 39
  • 55
2

This will do the job ...

List x = new ArrayList(Arrays.asList(arr_fav));
x.remove(String.valueOf(current_id));
arr_fav = x.toArray();
xagyg
  • 9,562
  • 2
  • 32
  • 29
1

Try something like this

int[] intAry = new int[5]; 

// populate array with 0 to 4  

for (int i=0; i < intAry.length; i++) {  

  intAry[i] = i;  

}  

List<Integer> aList  = Arrays.asList(intAry); // change the array to a list of integers  

aList.remove(3); // remove the item 3 (4th index)  

aList.toArray(intAry); // convert list back to array  

System.out.println("size of array=" + intAry.size()); // output array size should be 4  

for (int i=0; i < intAry.length; i++) {  

  System.out.print(intAry[i] + " "); // should output "0 1 2 4 "  

}  
duggu
  • 37,851
  • 12
  • 116
  • 113
curious
  • 371
  • 1
  • 9
1

try this:

    ArrayList<String> rm = new ArrayList<String>();
    rm .addAll(arr_fav);
    rm .remove(1);
G M Ramesh
  • 3,420
  • 9
  • 37
  • 53
0

You can do it using the following method..

public static String[] removeElements(String[] input, String deleteMe) {
List result = new LinkedList();

for(String item : input)
    if(!deleteMe.equals(item))
        result.add(item);

return result.toArray(input);
}

OR you could use ArrayUtils.

array = ArrayUtils.removeElement(array, element)
Nermeen
  • 15,883
  • 5
  • 59
  • 72
0

set

    array_fav[1]=array_fav[2];
    array_fav[2]=null;
Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36
0
    String[] arr_fav =
    { "1", "2", "3" };

    List<String> myList = Arrays.asList(arr_fav);

            String currentId = String.valueOf(current_id);
    for (int i = 0; i < arr_fav.length; i++)
    {
        if (arr_fav[i].equals(currentId))
        {
            myList.remove(i);
        }
    }
Mikhaili
  • 161
  • 6
0

For simple arrays like this you can't do this in this way

here is the full sample code for this

int current_id = 2;
        String[] arr_fav = { "1", "2", "3" };
        for (int i = 0; i < arr_fav.length; i++) {
            if (current_id == Integer.parseInt(arr_fav[i])) {
                String[] arr_fav_tem = new String[arr_fav.length - 1];
                arr_fav[1] = null;
                int counter = 0;
                for (int j = 0; j < arr_fav.length; j++) {
                    if (arr_fav[j] != null) {

                        arr_fav_tem[counter] = arr_fav[j];
                        counter++;
                    }

                }

                arr_fav = arr_fav_tem;

            }
        }

        for (int i = 0; i < arr_fav.length; i++) {
            System.out.println(arr_fav[i]);
        }
Ali Imran
  • 8,927
  • 3
  • 39
  • 50
0
 private String[] removeItem(String[] names,
            int position) {

        ArrayList<String> al_temp=new ArrayList<String>();// temporary ArrayList

    for(int i=0;i<names.length;i++)
        {
            al_temp.add(names[i]);
        }

        al_temp.remove(position);
        names= new String[al_temp.size()];//array cleared with new size



        for(int i=0;i<al_temp.size();i++)
        {
            names[i]=al_temp.get(i);
        }


        return names;
    }
Lins Louis
  • 2,393
  • 2
  • 25
  • 30
0

Copy this method:

private static String[] deleteElement(String stringToDelete, String[] array) {
    String[] result = new String[array.length];
    int index = 0;

     ArrayList<String> rm = new ArrayList<String>();

    for(int i = 0; i < array.length; i++) {
        rm.add(array[i]);
    }
    for(int i = 0; i < array.length; i++) {
        if(array[i].equals(poistettava)) {
            index = i;
        }
    }
    rm.remove(index);

    result = rm.toArray(new String[rm.size()]);

    return result;
}

To delete element:

String[] array = {"1", "2", "3"};
array = deleteElement("3", array);
Kivvil
  • 211
  • 3
  • 6