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.

3
  • 1
    You can convert it to arraylist Commented Dec 6, 2012 at 8:44
  • if you need something more general stackoverflow.com/a/122207/619713 Commented Dec 6, 2012 at 9:25
  • Optimization Parse one time current_id to string and equal it to arr_fav[i] Commented Dec 6, 2012 at 9:30

14 Answers 14

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()]);
Sign up to request clarification or add additional context in comments.

4 Comments

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, Here i am using arraylist instead of resizing array, as java does not provide resizing of its array by default. please check it carefuylly
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.
@Madhumitha - Yes it "works", but you really should read the above comment.
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.

4 Comments

-1 for "a List which is dynamic". The List interface does not require it being dynamic, it can even be immutable.
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.
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.
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".
5

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

1 Comment

check out my code.if you have more element you should use variables for swapping elements.
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];
 } }

Comments

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);

2 Comments

this is not exact answer. as he is checking the current_id with in array.
No, it is not an exact answer. It's an illustration how to use the remove() method of ArrayList, clearly.
2

This will do the job ...

List x = new ArrayList(Arrays.asList(arr_fav));
x.remove(String.valueOf(current_id));
arr_fav = x.toArray();

2 Comments

1. arr_fav is string array and you remove integer
Thanks for the tip. Updated to use String.valueOf.
1

try this:

    ArrayList<String> rm = new ArrayList<String>();
    rm .addAll(arr_fav);
    rm .remove(1);

Comments

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 "  

}  

Comments

0

set

    array_fav[1]=array_fav[2];
    array_fav[2]=null;

Comments

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)

2 Comments

it's internal class com.android.internal.util.ArrayUtils
Ah Ok but i think he develops for Android
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]);
        }

Comments

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);
        }
    }

Comments

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;
    }

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.