Home » Android » java – removing an element from String array in android

java – removing an element from String array in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&Answers:

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

Answer:

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.

Answer:

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

Answer:

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

Answer:

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

Answer:

This will do the job …

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

Answer:

try this:

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

Answer:

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 "  

}  

Answer:

set

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

Answer:

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)

Answer:

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

Answer:

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

Answer:

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

Answer:

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