从Java数组删除特定索引 [英] Remove specific index from array in java

查看:720
本文介绍了从Java数组删除特定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用一提的索引值删除阵列特定的元素?
例如,我可以删除字符 D 通过给首页值1?

 的char []字= {'C','D','F','H','J'};


解决方案

如果你不想使用ArrayList,
arraycopy 的一个替代方法:

  System.arraycopy(即0,结果0,I);
    System.arraycopy(即,I + 1,结果,我,result.length-i)的;

其中的 I 的是你的索引中删除。

希望我可以帮助。

修改:当然,你应该首先定义正确的数组的长度:

 的char []结果=新的char [words.length-1];

Can I remove a specific element from array by mentioning index value? For example can I remove the character d by giving index value 1?

char[] words = { 'c', 'd', 'f', 'h', 'j' };

解决方案

If you don't want to use ArrayList, arraycopy is an alternative:

    System.arraycopy(words, 0, result, 0, i);
    System.arraycopy(words, i+1, result, i, result.length-i);

where i is your index to delete.

Hope I can help.

EDIT: Of course you should initially define the correct array lengths:

char[] result = new char[words.length-1];

这篇关于从Java数组删除特定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆