Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在Swift 5.1中枚举期间从数组中删除元素而不进行反转_Arrays_Swift - Fatal编程技术网

Arrays 在Swift 5.1中枚举期间从数组中删除元素而不进行反转

Arrays 在Swift 5.1中枚举期间从数组中删除元素而不进行反转,arrays,swift,Arrays,Swift,如果for-in循环中的某个元素在某个条件下失败,我想删除它。目前,有一个解决方案,但它是旧的,似乎所有我试图做的复杂 在Java中,我可以这样引用索引: /* Program that removes negative numbers from an array */ //Make an array list and add some numbers to it. ArrayList<Int> a = new ArrayList<Int>; for (int i = 0

如果for-in循环中的某个元素在某个条件下失败,我想删除它。目前,有一个解决方案,但它是旧的,似乎所有我试图做的复杂

在Java中,我可以这样引用索引:

/* Program that removes negative numbers from an array */
//Make an array list and add some numbers to it.
ArrayList<Int> a = new ArrayList<Int>;
for (int i = 0; i < 10; i++){
    a.add( (int) ((Math.random - 0.5) * 100) //Add 10 numbers that are pos/neg.

//Remove all the negative numbers.
for (int i = 0; i < a.size; i++){
    if (a.get(i) < 0){
        a.remove(i); //Removes the element. 
    }
}
/*从数组中删除负数的程序*/
//制作一个数组列表并向其中添加一些数字。
ArrayList a=新的ArrayList;
对于(int i=0;i<10;i++){
a、 添加((int)((Math.random-0.5)*100)//添加10个正/负数字。
//删除所有负数。
for(int i=0;i
但在Swift中,据我所知,我必须为每个循环使用一个等效值:

    var array = [-4, -2, 0, 4, 6, 7, 2, 5, 7]
    //Iterate looking for numbers < 0.
    for item in array{
        if (item < 0){
            array.removeAt(item) //ERROR 
        }
        else{
            //Right now, I just copy every value into a new array.
            //But I shouldn't even need this w/ the Arrays collection.
    }
var数组=[-4,-2,0,4,6,7,2,5,7]
//迭代查找小于0的数字。
用于数组中的项{
如果(项目<0){
array.removeAt(item)//错误
}
否则{
//现在,我只是将每个值复制到一个新数组中。
//但我甚至不需要这个数组集合。
}
我怎么能做得更简单呢?对不起,我是个新手,谢谢你的帮助


编辑|澄清2020年6月14日晚上11:44:00:

有人建议这是一个很好的回答,但不是我想要的。(我的错,对不起)。我用这个作为一个例子,在我学习Swift的过程中,我正在研究一个不同的挑战

请帮助我寻找删除元素的解决方案。谢谢!!

使用
removeAll(where:)
方法如下:

array.removeAll(where: { $0 < 0 })
array.removeAll(其中:{$0<0})
或:

array.removeAll{$0<0}

在我上面评论的可能重复的问题链接上展开,您可以扩展SignedInteger协议并创建一个属性以基于条件返回布尔值。然后您可以将其用作RangeReplaceableCollection方法上的键路径,该方法删除满足给定谓词的所有元素

extension SignedInteger {
    var isNegative: Bool { self < 0 }
    var isPositive: Bool { self > 0 }
}


您可以将其删除。可以对正在迭代的序列进行变异,因为它是一个副本。当前的问题可能是重复的,您的问题是重复的。如果
filter
removeAll(其中:)
与您要查找的内容不符,请提交您的问题并用问题的清晰描述进行更新。非新:。
extension SignedInteger {
    var isNegative: Bool { self < 0 }
    var isPositive: Bool { self > 0 }
}
extension Numeric {
    var isZero: Bool { self == .zero }
}
var array = [-4, -2, 0, 4, 6, 7, 2, 5, 7]

array.removeAll(where: \.isNegative)
array  // [0, 4, 6, 7, 2, 5, 7]

array.removeAll(where: \.isZero)
array  // [4, 6, 7, 2, 5, 7]

array.removeAll(where: \.isPositive)
array  // []