如何从一个数组中删除特定元素 [英] How to remove specific element from an array
本文介绍了如何从一个数组中删除特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个字符数组测试不同的元素,如果他们不符合条件,我想从数组中删除它们。有没有办法做到这一点?
下面是我的code SOFAR
的String;
的char [] B = inputString.toCharArray();
B =新的char [b.length个]
做
{ 如果((二[Ⅰ]≥!='0')及及(B〔1] - ='9')及与放大器;!'。'(二[I] =))
{
S = Character.toString(B [I]);
如果(s.equals(+))
{
T = 1;
}
否则如果(s.equals( - ))
{
T = 2;
}
否则,如果//等等 }
其他
{
T = 1029;
}
//想在这里删除元素,如果他们失败,如果测试
}
解决方案
这应该做你想要什么:
的ArrayList<焦炭> charList =新的ArrayList<焦炭>(0);的for(int i = 0; I< b.length个;我++){
如果(B [I] ==条件){
(B [I])charList.Add;
}
}charList.toArray();
I have tested different elements in a char array and if they do not meet the conditions I want to remove them from the array. Is there a way to do this?
Here is my code sofar
String s;
char[] b = inputString.toCharArray();
b = new char[b.length];
do
{
if(!(b[i]>='0')&&(b[i]<='9')&&(b[i]!='.'))
{
s = Character.toString(b[i]);
if(s.equals("+"))
{
t = 1;
}
else if(s.equals("-"))
{
t = 2;
}
else if // and so on
}
else
{
t = 1029;
}
//want to delete element here if they fail if test
}
解决方案
This should do what you want:
ArrayList<char> charList = new ArrayList<char>(0);
for (int i= 0; i < b.length; i++) {
if (b[i] == condition) {
charList.Add(b[i]);
}
}
charList.toArray();
这篇关于如何从一个数组中删除特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文