android r.java string删除,从android中的String数组中删除一个元素

最好使用arraylist

arr_fav = {"1","2","3"}; Listnumlist = new ArrayList(); for(int i= 0;i

你没有。

数组无法resize。

您需要创建一个新的(较小的)数组,并将您希望保留的元素复制到其中。

更好的想法是使用动态的List实现。 例如, ArrayList 。

Java中的数组不是动态的 ,您可以使用ArrayList 。

您可以将所需的数组元素复制到新数组中

j = 0; for(int i= 0;i

使用ArrayList而不是数组。 它支持删除任何元素,动态大小等function。

ArrayListarr_fav_list = new ArrayList(); arr_fav_list.addAll(arr_fav); arr_fav_list.remove(1);

尝试这个:

ArrayListrm = new ArrayList(); rm .addAll(arr_fav); rm .remove(1);

这将完成工作……

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

尝试这样的事情

int[] intAry = new int[5]; // populate array with 0 to 4 for (int i=0; i < intAry.length; i++) { intAry[i] = i; } ListaList = 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 " }

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

您可以使用以下方法执行此操作..

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

或者您可以使用ArrayUtils 。

array = ArrayUtils.removeElement(array, element)

对于像这样的简单数组,你不能这样做

这是完整的示例代码

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

String[] arr_fav = { "1", "2", "3" }; ListmyList = 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); } }

private String[] removeItem(String[] names, int position) { ArrayListal_temp=new ArrayList();// temporary ArrayList for(int i=0;i

复制此方法:

private static String[] deleteElement(String stringToDelete, String[] array) { String[] result = new String[array.length]; int index = 0; ArrayListrm = new ArrayList(); 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; }

要删除元素:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值