java 如何从数组中删除特定元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11727381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove specific element from an array
提问by imulsion
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?
我已经测试了 char 数组中的不同元素,如果它们不满足条件,我想将它们从数组中删除。有没有办法做到这一点?
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
}
回答by alexgerst
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();
回答by Daniel Fischer
Because comments don't allow good code formatting:
因为注释不允许良好的代码格式:
At the beginning of your code, you get the String
contents as a char[]
and immediately lose it again by assigning a new char[]
of the same size to the variable.
在代码的开头,您将String
内容作为 a获取char[]
,然后通过char[]
为变量分配一个相同大小的 new 立即再次丢失它。
char[] b = inputString.toCharArray();
b = new char[b.length];
so the loop after works on a default-initialized array, not on the string contents. You need two array references to do the copying.
所以循环之后适用于默认初始化的数组,而不是字符串内容。您需要两个数组引用来进行复制。
回答by talnicolas
Something is not right with your program:
您的程序有问题:
String s;
char[] b = inputString.toCharArray();
b = new char[b.length];
do {
if(!(b[i]>='0')&&(b[i]<='9')&&(b[i]!='.')) {
...
}
} while...
You're creating a char[]
from your inputString
, and then on the next line you assign a whole new empty char[]
in your b
variable. b[i]
will in fact never be equals to 0
, 9
or i
. Did you put that line here by error?
您正在char[]
从 中创建 a inputString
,然后在下一行char[]
在b
变量中分配一个全新的空。b[i]
实际上将永远等于0
,9
或i
。你是不是错误地把那条线放在这里?
Then for the removing thing, I'd suggest too the use of an ArrayList where you will be able to iterate over it and remove the specific index you want to removevery easily.
然后对于删除的事情,我也建议使用 ArrayList,您可以在其中迭代它并非常轻松地删除要删除的特定索引。
回答by Noah
char[] finishedArray = new char[0];
char[] arrayToCheck = new char[]{ 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
for( int i = 0; i < arrayToCheck.length; i++ )
{
if( !doesNOTMeetSomeCondition( arrayToCheck[ i ] ) )
{
//DOES meet keep condition, add to new array
char[] newCharArray = new char[ finishedArray.length + 1 ];
for( int j = 0; j < finishedArray.length; j++ )
{
newCharArray[ j ] = finishedArray[ j ];
}
newCharArray[ finishedArray.length ] = arrayToCheck[ i ];
finishedArray = newCharArray;
}
}
//finishedArray contains desired result
A listwould be much more fitting tool if you can change the data structure and are comfortable using Strings to contain the chars.
一个列表会更配合工具,如果你可以改变数据结构和使用字符串包含的字符舒适。
List<String> finishedList = Arrays.asList( 'a', 'b', 'c', 'd', 'e', 'f', 'g' );
for( String charInList : finishedList )
{
if( doesNOTMeetSomeCondition( charInList.toCharArray()[ 0 ] ) )
{
finishedList.remove( charInList );
}
}
//finishedList contains the desired result
回答by SomeKittens
It'd probably be a better idea to use a switch
statement here. Rather than eliminating the character that you don't want from your array (mutation during iteration is evil unless you're using an Iterator
), why don't you use the StringBuilder
class to catch all the characters you dowant?
switch
在这里使用语句可能是一个更好的主意。而不是消除了,你不从你的阵列希望(突变时迭代是邪恶的,除非你正在使用的字符Iterator
),你为什么不使用StringBuilder
类来捕获所有你的人物都想要的吗?
回答by Zoop
You wont really be able to delete the element but you could change it by doing something like b[i] = 0; at the end of the given code. Arrays are a certain length and the length cant be changed so if you want to remove that part of the array i would suggest using a list instead.
您实际上无法删除该元素,但您可以通过执行诸如 b[i] = 0; 之类的操作来更改它。在给定代码的末尾。数组有一定的长度,长度不能改变,所以如果你想删除数组的那部分,我建议改用列表。
List temp = b.asList();
Iterator it = temp.iterator();
while(it.hasNext())
{
if(it.next() == "t") //your modification code here
it.remove();
}
char[] newB = temp.toArray();
Something like this would work for you i think.
我认为这样的事情对你有用。